public static byte[] HashRaw(EHashType type, byte[] bs) { if (bs == null) return null; HashAlgorithm cmd5 = null; switch (type) { case EHashType.Md5: cmd5 = new MD5CryptoServiceProvider(); break; case EHashType.Sha1: cmd5 = new SHA1CryptoServiceProvider(); break; case EHashType.Sha256: cmd5 = new SHA256CryptoServiceProvider(); break; case EHashType.Sha384: cmd5 = new SHA384CryptoServiceProvider(); break; case EHashType.Sha512: cmd5 = new SHA512CryptoServiceProvider(); break; } bs = cmd5.ComputeHash(bs); cmd5.Dispose(); return bs; }
public static byte[] HashRaw(EHashType type, Stream bs, bool seekBegin) { if (bs == null) return null; HashAlgorithm cmd5 = null; switch (type) { case EHashType.Md5: cmd5 = new MD5CryptoServiceProvider(); break; case EHashType.Sha1: cmd5 = new SHA1CryptoServiceProvider(); break; case EHashType.Sha256: cmd5 = new SHA256CryptoServiceProvider(); break; case EHashType.Sha384: cmd5 = new SHA384CryptoServiceProvider(); break; case EHashType.Sha512: cmd5 = new SHA512CryptoServiceProvider(); break; } if (seekBegin) bs.Seek(0, SeekOrigin.Begin); byte[] bsh = cmd5.ComputeHash(bs); cmd5.Dispose(); return bsh; }
static byte[] GenerateKey(string RawKey) { byte[] WhatToReturn; SHA512CryptoServiceProvider SCSP = new SHA512CryptoServiceProvider(); SHA256CryptoServiceProvider SCSP256 = new SHA256CryptoServiceProvider(); //Whatever the key is... hash it once. byte[] ReturnBytes = SCSP.ComputeHash(UnicodeEncoding.ASCII.GetBytes(RawKey.ToCharArray())); //Rehash the key up to 256 times depending on what key the user chose. WhatToReturn = ReturnBytes; for (byte x = 0; x <= ReturnBytes[0]; x++) WhatToReturn = SCSP.ComputeHash(WhatToReturn); //Make sure the key size is 32 bytes. WhatToReturn = SCSP256.ComputeHash(WhatToReturn); SCSP.Dispose(); SCSP256.Dispose(); //DISPOSE ALL SENSITIVE DATA Random rd = new Random(); rd.NextBytes(ReturnBytes); rd = null; GC.Collect(); //Finish return WhatToReturn; }