如何使用明文字符串和已知的salt生成md5哈希

时间:2015-05-30 21:39:39

标签: c# passwords md5 salt

我一直在疯狂地试图解决这个问题。 C#中是否有一种简单的方法可以使用" password123"和一个盐" vfs5%S] m(_ * Y + Tk"并生成一个单独的MD5哈希。基本上是网站http://free-online-web-tools.com/tool/md5所做的事情,但使用C#。

1 个答案:

答案 0 :(得分:0)

下面给出的函数为给定的纯文本值生成一个哈希值,并返回一个base64编码的结果。在计算哈希值之前,会生成随机盐并将其附加到纯文本中。此salt存储在哈希值的末尾,因此可以在以后用于哈希验证。

明文值要散列。该函数不检查此参数是否为空。

哈希算法的名称。允许的值为:“MD5”,“SHA1”,“SHA256”,“SHA384”和“SHA512”(如果指定了任何其他值,将使用MD5哈希算法)。该值不区分大小写。

在您的情况下,将hashAlgorithm设置为“MD5”

盐字节。此参数可以为null,在这种情况下将生成随机salt值。

散列值格式为base64编码的字符串。

public static string ComputeHash(string   plainText,
                                 string   hashAlgorithm,
                                 byte[]   saltBytes)
{
    // If salt is not specified, generate it on the fly.
    if (saltBytes == null)
    {
        // Define min and max salt sizes.
        int minSaltSize = 4;
        int maxSaltSize = 8;

        // Generate a random number for the size of the salt.
        Random  random = new Random();
        int saltSize = random.Next(minSaltSize, maxSaltSize);

        // Allocate a byte array, which will hold the salt.
        saltBytes = new byte[saltSize];

        // Initialize a random number generator.
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

        // Fill the salt with cryptographically strong byte values.
        rng.GetNonZeroBytes(saltBytes); 
    }

    // Convert plain text into a byte array.
    byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

    // Allocate array, which will hold plain text and salt.
    byte[] plainTextWithSaltBytes = 
            new byte[plainTextBytes.Length + saltBytes.Length];

    // Copy plain text bytes into resulting array.
    for (int i=0; i < plainTextBytes.Length; i++)
        plainTextWithSaltBytes[i] = plainTextBytes[i];

    // Append salt bytes to the resulting array.
    for (int i=0; i < saltBytes.Length; i++)
        plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];

    // Because we support multiple hashing algorithms, we must define
    // hash object as a common (abstract) base class. We will specify the
    // actual hashing algorithm class later during object creation.
    HashAlgorithm hash;

    // Make sure hashing algorithm name is specified.
    if (hashAlgorithm == null)
        hashAlgorithm = "";

    // Initialize appropriate hashing algorithm class.
    switch (hashAlgorithm.ToUpper())
    {
        case "SHA1":
            hash = new SHA1Managed();
            break;

        case "SHA256":
            hash = new SHA256Managed();
            break;

        case "SHA384":
            hash = new SHA384Managed();
            break;

        case "SHA512":
            hash = new SHA512Managed();
            break;

        default:
            hash = new MD5CryptoServiceProvider();
            break;
    }

    // Compute hash value of our plain text with appended salt.
    byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);

    // Create array which will hold hash and original salt bytes.
    byte[] hashWithSaltBytes = new byte[hashBytes.Length + 
                                        saltBytes.Length];

    // Copy hash bytes into resulting array.
    for (int i=0; i < hashBytes.Length; i++)
        hashWithSaltBytes[i] = hashBytes[i];

    // Append salt bytes to the result.
    for (int i=0; i < saltBytes.Length; i++)
        hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];

    // Convert result into a base64-encoded string.
    string hashValue = Convert.ToBase64String(hashWithSaltBytes);

    // Return the result.
    return hashValue;
}

有关详细信息refer here.