开发者

How to get a equal hash to the FormsAuthentication.HashPasswordForStoringInConfigFile("asdf", "MD5") method?

Looking for a method or to be pointed in the right direction so I can return an hash equal to the hash returned by FormsAuthentication.HashPasswordForStoringInConfigFile("asdf", "MD5"). I've been trying code like:

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

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

I'm not that strong on Hashing so I don't know 开发者_高级运维where to go next. I always end up with crazy special characters while the FormsAuth method always returns something readable.

Just trying to remove the external dependency to FormAuthentication from some internal business classes.


Here is the reflector's output:

Your problem is not using 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);
}

So here is your updated code:

    encoding.GetBytes("asdf");

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


After some Googling, I changed @Pieter's code to make it independent of the System.Web

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


This answer is derived from your answers:

This method in .NET is equivalent to sha1 in php:

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


The following is the actual code used to create the password with that method:

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


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜