Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获得FormsAuthentication.HashPasswordForStoringInConfigFile(“asdf”和“MD5”)方法的相等哈希?_C#_Asp.net_Hash - Fatal编程技术网

C# 如何获得FormsAuthentication.HashPasswordForStoringInConfigFile(“asdf”和“MD5”)方法的相等哈希?

C# 如何获得FormsAuthentication.HashPasswordForStoringInConfigFile(“asdf”和“MD5”)方法的相等哈希?,c#,asp.net,hash,C#,Asp.net,Hash,正在寻找一个指向正确方向的方法,以便返回一个哈希值,该哈希值等于FormsAuthentication.HashPasswordForStoringInConfigFile(“asdf”,“MD5”)返回的哈希值。我一直在尝试这样的代码: ASCIIEncoding encoding = new ASCIIEncoding(); encoding.GetBytes("asdf"); var hashedBytes = MD5.Create().C

正在寻找一个指向正确方向的方法,以便返回一个哈希值,该哈希值等于
FormsAuthentication.HashPasswordForStoringInConfigFile(“asdf”,“MD5”)
返回的哈希值。我一直在尝试这样的代码:

        ASCIIEncoding encoding = new ASCIIEncoding();
        encoding.GetBytes("asdf");

        var hashedBytes = MD5.Create().ComputeHash(bytes);
        var password = encoding.GetString(hashedBytes);
我不太擅长散列,所以我不知道下一步该去哪里。我总是以疯狂的特殊字符结束,而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);
}
下面是您的更新代码:

    encoding.GetBytes("asdf");

    var hashedBytes = MD5.Create().ComputeHash(bytes);
    var password = Encoding.UTF8.GetString(hashedBytes);

以下是用于使用该方法创建密码的实际代码:

System.Web.Configuration.MachineKeySection.ByteArrayToHexString(
    System.Security.Cryptography.MD5.Create().ComputeHash(
        Encoding.UTF8.GetBytes(password)
    ), 0
);

谷歌搜索之后,我修改了@Pieter的代码,使其独立于System.Web

return string.Join("",
  new MD5CryptoServiceProvider().ComputeHash(
    new MemoryStream(Encoding.UTF8.GetBytes(password))).Select(x => x.ToString("X2")));

此答案来源于您的答案:

NET中的此方法相当于php中的sha1:

string sha1Hash(string password)
{
    return string.Join("", SHA1CryptoServiceProvider.Create().ComputeHash(Encoding.UTF8.GetBytes(password)).Select(x => x.ToString("X2"))).ToLower();
}

但是现在您正在引入对System.Web的依赖关系。请注意,这不是存储密码散列的好方法。请参阅,如果需要的话,我将回答关于如何从该文件转换到PBKDF2的详细选项,全部在.NET中-本质上,要么将所有内容转换为RFC2898DeriveBytes(pw,salt,迭代),要么将所有内容转换为RFC2898DeriveBytes(HashPasswordForStoringInConfigFile(pw,“MD5”)、salt,迭代),或者在用户使用其他方法登录时升级用户
public static string sha1Hash(string password)
    {
           return string.Join("", MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(password)).Select(s => s.ToString("x2"))).ToLower();

    }
Random randomText = new Random();
 var algorithm = System.Security.Cryptography.HashAlgorithm.Create("MD5");
 byte[] hash = algorithm.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Convert.ToString(randomText.Next())));

// ToString("x2")  converts byte in hexadecimal value
string encryptedVal = string.Concat(hash.Select(b => b.ToString("x2"))).ToUpperInvariant();