下面是用于在我正在重写的应用程序中哈希密码的代码片段:
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个字节来存储数据.
原文地址:https://www.jb51.cc/csharp/98922.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。