下面是用于在我正在重写的应用程序中哈希密码的代码片段:
internal string GenerateSalt() { byte[] buf = new byte[16]; (new RNGCryptoServiceProvider()).GetBytes(buf); return Convert.ToBase64String(buf); } internal string EncodePassword(string pass,string salt) { byte[] bIn = Encoding.Unicode.GetBytes(pass); byte[] bSalt = Convert.FromBase64String(salt); byte[] bAll = new byte[bSalt.Length + bIn.Length]; Buffer.BlockCopy(bSalt,bAll,bSalt.Length); Buffer.BlockCopy(bIn,bSalt.Length,bIn.Length); HashAlgorithm s = HashAlgorithm.Create("SHA512"); byte[] bRet = s.ComputeHash(bAll); return Convert.ToBase64String(bRet); }
哈希密码和盐稍后存储在数据库中.什么是base64编码的盐和密码的最大长度?
解决方法
由于SHA-512哈希总是512位= 64字节而base64需要(n 2 – ((n 2)%3))/ 3 * 4字节,因此总是需要88个字节来存储数据.