从字符串计算MD5哈希值

时间:2012-07-12 14:26:02

标签: c# md5

我使用以下C#代码从字符串计算MD5哈希值。 它运行良好,生成一个32个字符的十六进制字符串,如下所示: 900150983cd24fb0d6963f7d28e17f72

string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";

//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);

// and then convert tmpHash to string...

有没有办法使用这样的代码生成一个16个字符的十六进制字符串(或12个字符的字符串)?一个32个字符的十六进制字符串是好的,但我认为客户输入代码会很无聊!

17 个答案:

答案 0 :(得分:145)

根据MSDN

创建MD5:

   public static string CreateMD5(string input)
    {
        // Use input string to calculate MD5 hash
        using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
        {
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hashBytes = md5.ComputeHash(inputBytes);

            // Convert the byte array to hexadecimal string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }

答案 1 :(得分:81)

// given, a password in a string
string password = @"1234abcd";

// byte array representation of that string
byte[] encodedPassword = new UTF8Encoding().GetBytes(password);

// need MD5 to calculate the hash
byte[] hash = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(encodedPassword);

// string representation (similar to UNIX format)
string encoded = BitConverter.ToString(hash)
   // without dashes
   .Replace("-", string.Empty)
   // make lowercase
   .ToLower();

// encoded contains the hash you are wanting

答案 2 :(得分:7)

完全取决于您想要实现的目标。从技术上讲,您可以从MD5哈希的结果中获取前12个字符,但MD5的规范是生成32个字符。

减少哈希的大小会降低安全性,并增加冲突和系统崩溃的可能性。

也许如果你让我们更多地了解你想要实现的目标,我们可能会提供更多帮助。

答案 3 :(得分:7)

您可以使用Convert.ToBase64String将MD5的16字节输出转换为~24字符串。在不降低安全性的情况下更好一点。 (j9JIbSY8HuT89/pwdC8jlw==为您的例子)

答案 4 :(得分:7)

尝试使用LINQ创建MD5哈希的字符串表示,但是,没有一个答案是LINQ解决方案,因此将其添加到可用解决方案的大杂烩中。

string result;
using (MD5 hash = MD5.Create())
{
    result = String.Join
    (
        "",
        from ba in hash.ComputeHash
        (
            Encoding.UTF8.GetBytes(observedText)
        ) 
        select ba.ToString("x2")
    );
}

答案 5 :(得分:5)

支持字符串和文件流。

实施例

string hashString = EasyMD5.Hash("My String");

string hashFile = EasyMD5.Hash(System.IO.File.OpenRead("myFile.txt"));

-

   class EasyMD5
        {
            private static string GetMd5Hash(byte[] data)
            {
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                    sBuilder.Append(data[i].ToString("x2"));
                return sBuilder.ToString();
            }

            private static bool VerifyMd5Hash(byte[] data, string hash)
            {
                return 0 == StringComparer.OrdinalIgnoreCase.Compare(GetMd5Hash(data), hash);
            }

            public static string Hash(string data)
            {
                using (var md5 = MD5.Create())
                    return GetMd5Hash(md5.ComputeHash(Encoding.UTF8.GetBytes(data)));
            }
            public static string Hash(FileStream data)
            {
                using (var md5 = MD5.Create())
                    return GetMd5Hash(md5.ComputeHash(data));
            }

            public static bool Verify(string data, string hash)
            {
                using (var md5 = MD5.Create())
                    return VerifyMd5Hash(md5.ComputeHash(Encoding.UTF8.GetBytes(data)), hash);
            }

            public static bool Verify(FileStream data, string hash)
            {
                using (var md5 = MD5.Create())
                    return VerifyMd5Hash(md5.ComputeHash(data), hash);
            }
        }

答案 6 :(得分:3)

MD5哈希是128位,因此您不能用十六进制表示少于32个字符...

答案 7 :(得分:3)

System.Text.StringBuilder hash = new System.Text.StringBuilder();
        System.Security.Cryptography.MD5CryptoServiceProvider md5provider = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bytes = md5provider.ComputeHash(new System.Text.UTF8Encoding().GetBytes(YourEntryString));

        for (int i = 0; i < bytes.Length; i++)
        {
            hash.Append(bytes[i].ToString("x2")); //lowerCase; X2 if uppercase desired
        }
        return hash.ToString();

答案 8 :(得分:3)

我认为最好在字符串MD5中使用UTF-8编码。

public static string MD5(this string s)
{
    using (var provider = System.Security.Cryptography.MD5.Create())
    {
        StringBuilder builder = new StringBuilder();                           

        foreach (byte b in provider.ComputeHash(Encoding.UTF8.GetBytes(s)))
            builder.Append(b.ToString("x2").ToLower());

        return builder.ToString();
    }
}

答案 9 :(得分:2)

这是我对UTF8的实用函数,如果需要,可以将其替换为ASCII

    public static byte[] MD5Hash(string message)
    {
        return MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(message));
    }

答案 10 :(得分:0)

StringBuilder sb= new StringBuilder();
for (int i = 0; i < tmpHash.Length; i++)
{
   sb.Append(tmpHash[i].ToString("x2"));
}

答案 11 :(得分:0)

此解决方案需要c#7.2并利用Span<T>的优势。无论输入大小如何,这都会分配固定数量的内存,并且比接受的答案要快。请注意,如果需要,您仍然需要调用.Replace("-", string.Empty).ToLowerInvariant()来格式化结果。

public static string CreateMD5(ReadOnlySpan<char> input)
{
    var encoding = System.Text.Encoding.UTF8;
    var inputByteCount = encoding.GetByteCount(input);

    using (var md5 = System.Security.Cryptography.MD5.Create())
    {
        Span<byte> bytes = stackalloc byte[inputByteCount];
        Span<byte> destination = stackalloc byte[md5.HashSize / 8];

        encoding.GetBytes(input, bytes);

        // checking the result is not required because this only returns false if "(destination.Length < HashSizeValue/8)", which is never true in this case
        md5.TryComputeHash(bytes, destination, out int _bytesWritten);

        return BitConverter.ToString(destination.ToArray());
    }
}

答案 12 :(得分:0)

https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.md5?view=netframework-4.7.2

using System;
using System.Security.Cryptography;
using System.Text;

    static string GetMd5Hash(string input)
            {
                using (MD5 md5Hash = MD5.Create())
                {

                    // Convert the input string to a byte array and compute the hash.
                    byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                    // Create a new Stringbuilder to collect the bytes
                    // and create a string.
                    StringBuilder sBuilder = new StringBuilder();

                    // Loop through each byte of the hashed data 
                    // and format each one as a hexadecimal string.
                    for (int i = 0; i < data.Length; i++)
                    {
                        sBuilder.Append(data[i].ToString("x2"));
                    }

                    // Return the hexadecimal string.
                    return sBuilder.ToString();
                }
            }

            // Verify a hash against a string.
            static bool VerifyMd5Hash(string input, string hash)
            {
                // Hash the input.
                string hashOfInput = GetMd5Hash(input);

                // Create a StringComparer an compare the hashes.
                StringComparer comparer = StringComparer.OrdinalIgnoreCase;

                return 0 == comparer.Compare(hashOfInput, hash);

            }

答案 13 :(得分:0)

.NET Core 2.1及更高版本的现有答案的更快替代方法:

public static string CreateMD5(string s)
{
    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
    {
        var encoding = Encoding.ASCII;
        var data = encoding.GetBytes(s);

        Span<byte> hashBytes = stackalloc byte[16];
        md5.TryComputeHash(data, hashBytes, out int written);
        if(written != hashBytes.Length)
            throw new OverflowException();


        Span<char> stringBuffer = stackalloc char[32];
        for (int i = 0; i < hashBytes.Length; i++)
        {
            hashBytes[i].TryFormat(stringBuffer.Slice(2 * i), out _, "x2");
        }
        return new string(stringBuffer);
    }
}

如果您确定您的字符串足够小并替换编码,则可以进一步优化它。用不安全的int GetBytes(ReadOnlySpan chars,Span bytes)替代方法替换GetBytes。

答案 14 :(得分:0)

我想提供一种替代方法,在我的测试(.NET 4.7.2)中,其性能似乎比craigdfrench's answer快至少10%:

<input type="checkbox" checked @bind="checkedValue">

如果您更喜欢在顶部放置public static string GetMD5Hash(string text) { using ( var md5 = MD5.Create() ) { byte[] computedHash = md5.ComputeHash( Encoding.UTF8.GetBytes(text) ); return new System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary(computedHash).ToString(); } } ,则可以使方法主体更易于阅读:

using System.Runtime.Remoting.Metadata.W3cXsd2001;

足够明显,但出于完整性考虑,在OP的上下文中,它将用作:

using ( var md5 = MD5.Create() )
{
    return new SoapHexBinary( md5.ComputeHash( Encoding.UTF8.GetBytes(text) ) ).ToString();
}

答案 15 :(得分:0)

了解大约16个字符的十六进制字符串。...

using System;
using System.Security.Cryptography;
using System.Text;

但是这是我的一行创建MD5哈希的方法。

string hash = BitConverter.ToString(MD5.Create().ComputeHash(Encoding.ASCII.GetBytes("THIS STRING TO MD5"))).Replace("-","");

答案 16 :(得分:0)

扩展Anant Dabhianswer

辅助方法:

using System.Text;

namespace XYZ.Helpers
{
    public static class EncryptionHelper
    {
        public static string ToMD5(this string input)
        {
            // Use input string to calculate MD5 hash
            using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
            {
                byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                // Convert the byte array to hexadecimal string
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("X2"));
                }
                return sb.ToString();
            }
        }
    }
}