using System; using System.Collections; using System.Globalization; 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 CalendarManager { public const int DATE = 2; public const int DAY_OF_MONTH = 7; public const int HOUR = 3; public const int HOUR_OF_DAY = 8; public static CalendarHashTable manager = new CalendarHashTable(); public const int MILLISECOND = 6; public const int MINUTE = 4; public const int MONTH = 1; public const int SECOND = 5; public const int YEAR = 0; public class CalendarHashTable : Hashtable { public void Clear(Calendar calendar) { if (this[calendar] != null) { this.Remove(calendar); } } public void Clear(Calendar calendar, int field) { if (this[calendar] != null) { this.Remove(calendar); } else { this.Set(calendar, field, 0); } } public int Get(Calendar calendar, int field) { if (this[calendar] != null) { switch (field) { case 0: return ((CalendarProperties)this[calendar]).dateTime.Year; case 1: return (((CalendarProperties)this[calendar]).dateTime.Month - 1); case 2: return ((CalendarProperties)this[calendar]).dateTime.Day; case 3: { int hour = ((CalendarProperties)this[calendar]).dateTime.Hour; if (hour > 12) { return (hour - 12); } return hour; } case 4: return ((CalendarProperties)this[calendar]).dateTime.Minute; case 5: return ((CalendarProperties)this[calendar]).dateTime.Second; case 6: return ((CalendarProperties)this[calendar]).dateTime.Millisecond; case 7: return ((CalendarProperties)this[calendar]).dateTime.Day; case 8: return ((CalendarProperties)this[calendar]).dateTime.Hour; } return 0; } CalendarProperties properties = new CalendarProperties { dateTime = DateTime.Now }; this.Add(calendar, properties); return this.Get(calendar, field); } public DateTime GetDateTime(Calendar calendar) { if (this[calendar] != null) { return ((CalendarProperties)this[calendar]).dateTime; } CalendarProperties properties = new CalendarProperties { dateTime = DateTime.Now }; this.Add(calendar, properties); return this.GetDateTime(calendar); } public DayOfWeek GetFirstDayOfWeek(Calendar calendar) { if ((this[calendar] != null) && (((CalendarProperties)this[calendar]).dateTimeFormat != null)) { return ((CalendarProperties)this[calendar]).dateTimeFormat.FirstDayOfWeek; } CalendarProperties properties = new CalendarProperties { dateTimeFormat = new DateTimeFormatInfo() }; properties.dateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday; this.Add(calendar, properties); return this.GetFirstDayOfWeek(calendar); } public void Set(Calendar calendar, int field, int fieldValue) { if (this[calendar] == null) { CalendarProperties properties = new CalendarProperties { dateTime = DateTime.Now }; this.Add(calendar, properties); this.Set(calendar, field, fieldValue); } else { DateTime dateTime = ((CalendarProperties)this[calendar]).dateTime; switch (field) { case 0: dateTime = dateTime.AddYears(fieldValue - dateTime.Year); break; case 1: dateTime = dateTime.AddMonths(fieldValue - (dateTime.Month + 1)); break; case 2: dateTime = dateTime.AddDays((double)(fieldValue - dateTime.Day)); break; case 3: dateTime = dateTime.AddHours((double)(fieldValue - dateTime.Hour)); break; case 4: dateTime = dateTime.AddMinutes((double)(fieldValue - dateTime.Minute)); break; case 5: dateTime = dateTime.AddSeconds((double)(fieldValue - dateTime.Second)); break; case 6: dateTime = dateTime.AddMilliseconds((double)(fieldValue - dateTime.Millisecond)); break; case 7: dateTime = dateTime.AddDays((double)(fieldValue - dateTime.Day)); break; case 8: dateTime = dateTime.AddHours((double)(fieldValue - dateTime.Hour)); break; } ((CalendarProperties)this[calendar]).dateTime = dateTime; } } public void Set(Calendar calendar, int year, int month, int day) { if (this[calendar] != null) { this.Set(calendar, 0, year); this.Set(calendar, 1, month); this.Set(calendar, 2, day); } else { CalendarProperties properties = new CalendarProperties { dateTime = new DateTime(year, month + 1, day) }; this.Add(calendar, properties); } } public void Set(Calendar calendar, int year, int month, int day, int hour, int minute) { if (this[calendar] != null) { this.Set(calendar, 0, year); this.Set(calendar, 1, month); this.Set(calendar, 2, day); this.Set(calendar, 3, hour); this.Set(calendar, 4, minute); } else { CalendarProperties properties = new CalendarProperties { dateTime = new DateTime(year, month + 1, day, hour, minute, 0) }; this.Add(calendar, properties); } } public void Set(Calendar calendar, int year, int month, int day, int hour, int minute, int second) { if (this[calendar] != null) { this.Set(calendar, 0, year); this.Set(calendar, 1, month); this.Set(calendar, 2, day); this.Set(calendar, 3, hour); this.Set(calendar, 4, minute); this.Set(calendar, 5, second); } else { CalendarProperties properties = new CalendarProperties { dateTime = new DateTime(year, month + 1, day, hour, minute, second) }; this.Add(calendar, properties); } } public void SetDateTime(Calendar calendar, DateTime date) { if (this[calendar] != null) { ((CalendarProperties)this[calendar]).dateTime = date; } else { CalendarProperties properties = new CalendarProperties { dateTime = date }; this.Add(calendar, properties); } } public void SetFirstDayOfWeek(Calendar calendar, DayOfWeek firstDayOfWeek) { if ((this[calendar] != null) && (((CalendarProperties)this[calendar]).dateTimeFormat != null)) { ((CalendarProperties)this[calendar]).dateTimeFormat.FirstDayOfWeek = firstDayOfWeek; } else { CalendarProperties properties = new CalendarProperties { dateTimeFormat = new DateTimeFormatInfo() }; this.Add(calendar, properties); this.SetFirstDayOfWeek(calendar, firstDayOfWeek); } } public void SetTimeInMilliseconds(Calendar calendar, long milliseconds) { if (this[calendar] != null) { ((CalendarProperties)this[calendar]).dateTime = new DateTime(milliseconds); } else { CalendarProperties properties = new CalendarProperties { dateTime = new DateTime(0x2710L * milliseconds) }; this.Add(calendar, properties); } } private class CalendarProperties { public DateTime dateTime; public DateTimeFormatInfo dateTimeFormat; } } } 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 {} }