private NewPassword HashPassword(NewPassword np) { HashAlgorithm hashAlg = null; try { hashAlg = new SHA256CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(np.GetSaltPassword()); byte[] bytHash = hashAlg.ComputeHash(bytValue); np.SaltedHashedPassword = Convert.ToBase64String(bytHash); } catch (Exception e) { throw e; } finally { if (hashAlg != null) { hashAlg.Clear(); hashAlg.Dispose(); hashAlg = null; } } return np; }
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; }