00001 using System;
00002
00003 namespace Tamir.SharpSsh.jsch.jce
00004 {
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 public class SignatureDSA : Tamir.SharpSsh.jsch.SignatureDSA
00035 {
00036
00037
00038
00039 System.Security.Cryptography.DSAParameters DSAKeyInfo;
00040 System.Security.Cryptography.SHA1CryptoServiceProvider sha1;
00041 System.Security.Cryptography.CryptoStream cs;
00042
00043 public void init()
00044 {
00045 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
00046 cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, sha1, System.Security.Cryptography.CryptoStreamMode.Write);
00047 }
00048 public void setPubKey(byte[] y, byte[] p, byte[] q, byte[] g)
00049 {
00050 DSAKeyInfo.Y = Util.stripLeadingZeros( y );
00051 DSAKeyInfo.P = Util.stripLeadingZeros( p ) ;
00052 DSAKeyInfo.Q = Util.stripLeadingZeros( q );
00053 DSAKeyInfo.G = Util.stripLeadingZeros( g ) ;
00054 }
00055 public void setPrvKey(byte[] x, byte[] p, byte[] q, byte[] g)
00056 {
00057 DSAKeyInfo.X = Util.stripLeadingZeros( x );
00058 DSAKeyInfo.P = Util.stripLeadingZeros( p );
00059 DSAKeyInfo.Q = Util.stripLeadingZeros( q );
00060 DSAKeyInfo.G = Util.stripLeadingZeros( g );
00061 }
00062
00063 public byte[] sign()
00064 {
00065
00066 cs.Close();
00067 System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider();
00068 DSA.ImportParameters(DSAKeyInfo);
00069 System.Security.Cryptography.DSASignatureFormatter DSAFormatter = new System.Security.Cryptography.DSASignatureFormatter(DSA);
00070 DSAFormatter.SetHashAlgorithm("SHA1");
00071
00072 byte[] sig =DSAFormatter.CreateSignature( sha1 );
00073 return sig;
00074 }
00075 public void update(byte[] foo)
00076 {
00077
00078 cs.Write( foo , 0, foo.Length);
00079 }
00080 public bool verify(byte[] sig)
00081 {
00082 cs.Close();
00083 System.Security.Cryptography.DSACryptoServiceProvider DSA = new System.Security.Cryptography.DSACryptoServiceProvider();
00084 DSA.ImportParameters(DSAKeyInfo);
00085 System.Security.Cryptography.DSASignatureDeformatter DSADeformatter = new System.Security.Cryptography.DSASignatureDeformatter(DSA);
00086 DSADeformatter.SetHashAlgorithm("SHA1");
00087
00088 long i=0;
00089 long j=0;
00090 byte[] tmp;
00091
00092
00093 if(sig[0]==0 && sig[1]==0 && sig[2]==0)
00094 {
00095 long i1 = (sig[i++]<<24)&0xff000000;
00096 long i2 = (sig[i++]<<16)&0x00ff0000;
00097 long i3 = (sig[i++]<<8)&0x0000ff00;
00098 long i4 = (sig[i++])&0x000000ff;
00099 j = i1 | i2 | i3 | i4;
00100
00101 i+=j;
00102
00103 i1 = (sig[i++]<<24)&0xff000000;
00104 i2 = (sig[i++]<<16)&0x00ff0000;
00105 i3 = (sig[i++]<<8)&0x0000ff00;
00106 i4 = (sig[i++])&0x000000ff;
00107 j = i1 | i2 | i3 | i4;
00108
00109 tmp=new byte[j];
00110 Array.Copy(sig, i, tmp, 0, j); sig=tmp;
00111 }
00112 bool res = DSADeformatter.VerifySignature(sha1, sig);
00113 return res;
00114 }
00115 }
00116
00117 }