Hash函数.NET [英] Hash Function .NET

查看:161
本文介绍了Hash函数.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该写一个应用程序,需要字符串输入和计算哈希值的字符串(输入的maximun字符为16),输出应该是在Base64格式的22个字符(或更少而不是更多)长

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.

我看到.NET框架提出许多散列函数,我不知道干什么用的,
谁能请我推荐什么是最好的功能使用了,我怎么能输出限制为22个字符?

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?

感谢

推荐答案

您可以使用MD5这给128位输出,然后扔掉的最后两个字符当转换为Base64,因为他们永远是==(填充)。这应该给你22个字符。

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

这篇关于Hash函数.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆