Skip to main content

.net 2.0哈希字符串

存储用户密码到数据库,一般都要求先对密码字符串加密。在.net中常用的是MD5、SHA等不可逆的HASH算法。using System.Security.Cryptography;

private string EncodePassword(string password, string hashType)
{
  HashAlgorithm hash = HashAlgorithm.Create(hashType);
    byte[] bytes = Encoding.Unicode.GetBytes(password);
    byte[] inArray = hash.ComputeHash(bytes);

  return Convert.ToBase64String(inArray);
}

可选的HASH类型有:"SHA","SHA1","MD5","SHA256","SHA384","SHA512"

好像通杀: 2.0, 1.1, 1.0。我在2.0下测试通过。

Comments