我需要编写一个应用程序,接受字符串输入并计算字符串的哈希值(输入字符串的最大字符数为16),输出应以base64格式为22个字符长度(或更短但不更长)。
我发现.NET框架提供了许多哈希函数,但我不知道该使用哪个,有人能推荐一下最好的函数以及如何限制输出为22个字符吗?
谢谢!
我需要编写一个应用程序,接受字符串输入并计算字符串的哈希值(输入字符串的最大字符数为16),输出应以base64格式为22个字符长度(或更短但不更长)。
我发现.NET框架提供了许多哈希函数,但我不知道该使用哪个,有人能推荐一下最好的函数以及如何限制输出为22个字符吗?
谢谢!
string GetEncodedHash(string password, string salt)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte [] digest = md5.ComputeHash(Encoding.UTF8.GetBytes(password + salt);
string base64digest = Convert.ToBase64String(digest, 0, digest.Length);
return base64digest.Substring(0, base64digest.Length-2);
}
string secretKey = "MySecretKey";
string salt = "123";
System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
byte[] preHash = System.Text.Encoding.UTF32.GetBytes(secretKey + salt);
byte[] hash = sha.ComputeHash(preHash);
string password = prefix + System.Convert.ToBase64String(hash, 0, 15);
22个base64字符意味着从您的哈希函数输出16字节;您可以使用任何输出128位的哈希函数。