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[] receiver = new byte[target.Length]; int bytesRead = sourceStream.Read(receiver, start, count); for (int i = start; i < (start + bytesRead); i++) { target[i] = (sbyte) receiver[i]; } return bytesRead; } public static int ReadInput(TextReader sourceTextReader, ref sbyte[] target, int start, int count) { char[] charArray = new char[target.Length]; int bytesRead = sourceTextReader.Read(charArray, start, count); for (int index = start; index < (start + bytesRead); index++) { target[index] = (sbyte) charArray[index]; } return bytesRead; } public static byte[] ToByteArray(string sourceString) { byte[] byteArray = new byte[sourceString.Length]; for (int index = 0; index < sourceString.Length; index++) { byteArray[index] = (byte) sourceString[index]; } return byteArray; } public static byte[] ToByteArray(byte[] sbyteArray) { byte[] byteArray = new byte[sbyteArray.Length]; for (int index = 0; index < sbyteArray.Length; index++) { byteArray[index] = sbyteArray[index]; } return byteArray; } public static char[] ToCharArray(byte[] byteArray) { char[] charArray = new char[byteArray.Length]; byteArray.CopyTo(charArray, 0); return charArray; } public static char[] ToCharArray(sbyte[] sByteArray) { char[] charArray = new char[sByteArray.Length]; sByteArray.CopyTo(charArray, 0); return charArray; } public static sbyte[] ToSByteArray(byte[] byteArray) { sbyte[] sbyteArray = new sbyte[byteArray.Length]; for (int index = 0; index < byteArray.Length; index++) { sbyteArray[index] = (sbyte) byteArray[index]; } return sbyteArray; } 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 SupportClass.DigitalSignature GetInstance(string algorithmName) { if (!algorithmName.ToLower().Equals("sha1withdsa") && !algorithmName.ToLower().Equals("shawithdsa")) { throw new Exception("Algorithm not supported"); } return new SupportClass.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[] realSignature = null; if (this.objective != 1) { throw new Exception("Object was not created for signing"); } realSignature = this.formatter.CreateSignature(this.Data); this.Reset(); return realSignature; } public void Signing() { this.objective = 1; } public override string ToString() { string result = "Instance of DigitalSignature for "; if (this.objective == 1) { result = result + "signing "; } else { result = result + "verification "; } return (result + "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[] oldData = this.Data; this.Data = new byte[(newData.Length + this.position) + 1]; oldData.CopyTo(this.Data, 0); byte[] hashedNew = newData; this.hashAlgorithm.TransformBlock(newData, 0, hashedNew.Length, hashedNew, 0); hashedNew.CopyTo(this.Data, oldData.Length); this.position = this.Data.Length - 1; } } public void Update(byte newData) { byte[] newDataArray = new byte[] { newData }; this.Update(newDataArray); } public void Update(byte[] newData, int offset, int count) { byte[] newDataArray = new byte[count]; Array.Copy(newData, offset, newDataArray, 0, count); this.Update(newDataArray); } public void Verification() { this.objective = 2; } public bool Verify(byte[] signature) { bool result = false; if (this.objective != 2) { throw new Exception("Object was not created for verification"); } result = this.deformatter.VerifySignature(this.Data, signature); this.Reset(); return result; } public string AlgorithmName { get { return this.algorithmName; } } public byte[] Data { get { return this.data; } set { this.data = value; } } } public class KeySupport { private 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 : SupportClass.KeySupport { } public class PublicKeySupport : SupportClass.KeySupport { } }