Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I should write an app that takes string input and computes the hash value for the string (the maximun characters of the input is 16), the output should be in length of 22 characters (or less but not more) on base64 format.

I see that .NET framework suggests many hash functions, and I have no idea what to use, can anyone please recommend me what is the best function to use, and how can I limit the output to 22 characters?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
275 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use MD5 which gives a 128 bit output and then throw away the last two characters when converted to base64, as they'll always be "==" (padding). This should give you 22 characters.

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...