寻找一个方法或指向正确的方向,这样我就可以返回一个等于返回的哈希的哈希值FormsAuthentication.HashPasswordForStoringInConfigFile("asdf", "MD5").我一直在尝试代码:
ASCIIEncoding encoding = new ASCIIEncoding();
encoding.GetBytes("asdf");
var hashedBytes = MD5.Create().ComputeHash(bytes);
var password = encoding.GetString(hashedBytes);
Run Code Online (Sandbox Code Playgroud)
我对Hashing并不那么强大,所以我不知道下一步该去哪里.我总是以疯狂的特殊字符结束,而FormsAuth方法总是返回可读的东西.
只是尝试从一些内部业务类中删除对FormAuthentication的外部依赖项.
这是反射器的输出:
您的问题不是使用UTF8
public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat)
{
HashAlgorithm algorithm;
if (password == null)
{
throw new ArgumentNullException("password");
}
if (passwordFormat == null)
{
throw new ArgumentNullException("passwordFormat");
}
if (StringUtil.EqualsIgnoreCase(passwordFormat, "sha1"))
{
algorithm = SHA1.Create();
}
else
{
if (!StringUtil.EqualsIgnoreCase(passwordFormat, "md5"))
{
throw new ArgumentException(SR.GetString("InvalidArgumentValue", new object[] { "passwordFormat" }));
}
algorithm = MD5.Create();
}
return MachineKeySection.ByteArrayToHexString(algorithm.ComputeHash(Encoding.UTF8.GetBytes(password)), 0);
}
Run Code Online (Sandbox Code Playgroud)
所以这是您更新的代码:
encoding.GetBytes("asdf");
var hashedBytes = MD5.Create().ComputeHash(bytes);
var password = Encoding.UTF8.GetString(hashedBytes);
Run Code Online (Sandbox Code Playgroud)
经过一些谷歌搜索,我改变了@ Pieter的代码,使其独立于System.Web
return string.Join("",
new MD5CryptoServiceProvider().ComputeHash(
new MemoryStream(Encoding.UTF8.GetBytes(password))).Select(x => x.ToString("X2")));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11345 次 |
| 最近记录: |