//----------------------------------------------------------------------- // // HashMgr belongs to Copyright (c) Origtek. All rights reserved. // //----------------------------------------------------------------------- namespace OT.COM.Encryption { using System; using System.Security.Cryptography; /// /// Manager of Hash function /// public partial class HashMgr { /// /// For choose hash type /// public enum HashType { /// /// MD5 algorithm /// HT_MD5 = 0, /// /// SHA1 algorithm /// HT_SHA1 = 1, /// /// SHA256 algorithm /// HT_SHA256 = 2, /// /// SHA384 algorithm /// HT_SHA384 = 3, /// /// SHA512 algorithm /// HT_SHA512 = 4, } /// /// Generate hash function /// /// /// Hash type /// /// /// string data /// /// /// Result string /// public string Generate(HashType i_htType, string i_sData) { return System.Text.Encoding.Unicode.GetString( this.Gernerate( i_htType, System.Text.Encoding.Unicode.GetBytes(i_sData))); } /// /// Base function of Generate /// /// /// Hash type /// /// /// Byte array data /// /// /// Result byte array /// public byte[] Gernerate(HashType i_htType, byte[] i_byData) { // This is one implementation of the abstract class MD5. string sName = Enum.GetName(typeof(HashType), i_htType); byte[] result = null; object oRes = null; string s = string.Format("System.Security.Cryptography.{0}CryptoServiceProvider", sName.Substring(3)); switch (i_htType) { case HashType.HT_SHA256: { oRes = new SHA256CryptoServiceProvider(); } break; case HashType.HT_SHA384: { oRes = new SHA384CryptoServiceProvider(); } break; case HashType.HT_SHA512: { oRes = new SHA512CryptoServiceProvider(); } break; default: { } break; } if (oRes != null && oRes is HashAlgorithm) { result = (oRes as HashAlgorithm).ComputeHash(i_byData); } return result; } } }