using System; using System.IO; using System.Security.Cryptography; public class SupportClass { public static int ReadInput(Stream sourceStream, ref sbyte[] target, int start, int count) { byte[] buffer = new byte[target.Length]; int num = sourceStream.Read(buffer, start, count); for (int i = start; i < (start + num); i++) { target[i] = (sbyte)buffer[i]; } return num; } public static int ReadInput(TextReader sourceTextReader, ref sbyte[] target, int start, int count) { char[] buffer = new char[target.Length]; int num = sourceTextReader.Read(buffer, start, count); for (int i = start; i < (start + num); i++) { target[i] = (sbyte)buffer[i]; } return num; } public static byte[] ToByteArray(string sourceString) { byte[] buffer = new byte[sourceString.Length]; for (int i = 0; i < sourceString.Length; i++) { buffer[i] = (byte)sourceString[i]; } return buffer; } public static byte[] ToByteArray(byte[] sbyteArray) { byte[] buffer = new byte[sbyteArray.Length]; for (int i = 0; i < sbyteArray.Length; i++) { buffer[i] = sbyteArray[i]; } return buffer; } public static char[] ToCharArray(byte[] byteArray) { char[] array = new char[byteArray.Length]; byteArray.CopyTo(array, 0); return array; } public static char[] ToCharArray(sbyte[] sByteArray) { char[] array = new char[sByteArray.Length]; sByteArray.CopyTo(array, 0); return array; } public static sbyte[] ToSByteArray(byte[] byteArray) { sbyte[] numArray = new sbyte[byteArray.Length]; for (int i = 0; i < byteArray.Length; i++) { numArray[i] = (sbyte)byteArray[i]; } return numArray; } public static void WriteStackTrace(Exception throwable, TextWriter stream) { stream.Write(throwable.StackTrace); stream.Flush(); } public class DigitalSignature { private string algorithmName; private byte[] data; private AsymmetricSignatureDeformatter deformatter; private AsymmetricSignatureFormatter formatter; private HashAlgorithm hashAlgorithm; private int objective; private int position; protected DigitalSignature() {} public static DigitalSignature GetInstance(string algorithmName) { if (!algorithmName.ToLower().Equals("sha1withdsa") && !algorithmName.ToLower().Equals("shawithdsa")) { throw new Exception("Algorithm not supported"); } return new DigitalSignature { formatter = new DSASignatureFormatter(), deformatter = new DSASignatureDeformatter(), hashAlgorithm = new SHA1Managed(), algorithmName = "SHAwithDSA", objective = 0 }; } private void Reset() { this.data = null; this.position = 0; } public byte[] Sign() { byte[] buffer = null; if (this.objective != 1) { throw new Exception("Object was not created for signing"); } buffer = this.formatter.CreateSignature(this.Data); this.Reset(); return buffer; } public void Signing() { this.objective = 1; } public override string ToString() { string str = "Instance of DigitalSignature for "; if (this.objective == 1) { str = str + "signing "; } else { str = str + "verification "; } return (str + "using " + this.AlgorithmName + " algorithm"); } public void Update(byte[] newData) { if (this.position == 0) { this.Data = newData; this.hashAlgorithm.TransformBlock(newData, 0, newData.Length, this.Data, 0); this.position = this.Data.Length - 1; } else { byte[] data = this.Data; this.Data = new byte[(newData.Length + this.position) + 1]; data.CopyTo(this.Data, 0); byte[] outputBuffer = newData; this.hashAlgorithm.TransformBlock(newData, 0, outputBuffer.Length, outputBuffer, 0); outputBuffer.CopyTo(this.Data, data.Length); this.position = this.Data.Length - 1; } } public void Update(byte newData) { byte[] buffer = new byte[] {newData}; this.Update(buffer); } public void Update(byte[] newData, int offset, int count) { byte[] destinationArray = new byte[count]; Array.Copy(newData, offset, destinationArray, 0, count); this.Update(destinationArray); } public void Verification() { this.objective = 2; } public bool Verify(byte[] signature) { bool flag = false; if (this.objective != 2) { throw new Exception("Object was not created for verification"); } flag = this.deformatter.VerifySignature(this.Data, signature); this.Reset(); return flag; } public string AlgorithmName { get { return this.algorithmName; } } public byte[] Data { get { return this.data; } set { this.data = value; } } } public class KeySupport { private readonly KeyedHashAlgorithm algorithm; public KeySupport() {} public KeySupport(KeyedHashAlgorithm algorithm) { this.algorithm = algorithm; } public string GetAlgorithm() { return this.algorithm.ToString(); } public byte[] Key { get { return this.algorithm.Key; } } } public class PrivateKeySupport : KeySupport {} public class PublicKeySupport : KeySupport {} }