You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求推荐适用于Blazor (WebAssembly)的.NET MD5 NuGet库及替代方案

Blazor环境下Gravatar场景的MD5替代方案

问题背景

.NET原生的System.Security.Cryptography.MD5类无法在Blazor环境中使用,且没有适配的第三方库可用,现需实现MD5哈希用于Gravatar头像获取(非密码哈希场景),优先避免JS互操作。

纯C#实现MD5方案

直接在项目中添加纯C#编写的MD5算法实现,以下是可直接使用的代码:

public static class Md5Helper
{
    public static string ComputeMd5Hash(string input)
    {
        if (string.IsNullOrEmpty(input))
            return string.Empty;

        // Gravatar要求邮箱先转小写并去除前后空格
        input = input.Trim().ToLowerInvariant();
        
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        byte[] hashBytes = ComputeMd5(inputBytes);

        // 将字节数组转换为十六进制字符串
        StringBuilder sb = new StringBuilder();
        foreach (byte b in hashBytes)
        {
            sb.Append(b.ToString("x2"));
        }
        return sb.ToString();
    }

    private static byte[] ComputeMd5(byte[] inputBytes)
    {
        int[] s = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
                    5,  9, 14, 20, 5,  9, 14, 20, 5,  9, 14, 20, 5,  9, 14, 20,
                    4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
                    6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 };

        uint[] k = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
                     0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
                     0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
                     0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
                     0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
                     0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
                     0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
                     0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
                     0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
                     0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
                     0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
                     0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
                     0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
                     0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
                     0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
                     0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };

        uint a0 = 0x67452301;
        uint b0 = 0xefcdab89;
        uint c0 = 0x98badcfe;
        uint d0 = 0x10325476;

        byte[] paddedInput = PadInput(inputBytes);
        uint[] blocks = new uint[paddedInput.Length / 4];

        for (int i = 0; i < blocks.Length; i++)
        {
            blocks[i] = BitConverter.ToUInt32(paddedInput, i * 4);
        }

        for (int i = 0; i < blocks.Length; i += 16)
        {
            uint a = a0;
            uint b = b0;
            uint c = c0;
            uint d = d0;

            for (int j = 0; j < 64; j++)
            {
                uint f, g;

                if (j < 16)
                {
                    f = (b & c) | (~b & d);
                    g = (uint)j;
                }
                else if (j < 32)
                {
                    f = (d & b) | (~d & c);
                    g = (uint)((5 * j + 1) % 16);
                }
                else if (j < 48)
                {
                    f = b ^ c ^ d;
                    g = (uint)((3 * j + 5) % 16);
                }
                else
                {
                    f = c ^ (b | ~d);
                    g = (uint)((7 * j) % 16);
                }

                uint temp = d;
                d = c;
                c = b;
                b = b + RotateLeft(a + f + k[j] + blocks[i + (int)g], s[j]);
                a = temp;
            }

            a0 += a;
            b0 += b;
            c0 += c;
            d0 += d;
        }

        byte[] hashBytes = new byte[16];
        Buffer.BlockCopy(BitConverter.GetBytes(a0), 0, hashBytes, 0, 4);
        Buffer.BlockCopy(BitConverter.GetBytes(b0), 0, hashBytes, 4, 4);
        Buffer.BlockCopy(BitConverter.GetBytes(c0), 0, hashBytes, 8, 4);
        Buffer.BlockCopy(BitConverter.GetBytes(d0), 0, hashBytes, 12, 4);

        return hashBytes;
    }

    private static byte[] PadInput(byte[] inputBytes)
    {
        long originalLength = inputBytes.Length;
        long paddingLength = (448 - (originalLength * 8 + 1) % 512 + 512) % 512;
        paddingLength /= 8;

        byte[] paddedInput = new byte[originalLength + paddingLength + 8];
        inputBytes.CopyTo(paddedInput, 0);
        paddedInput[originalLength] = 0x80;

        long bitLength = originalLength * 8;
        byte[] lengthBytes = BitConverter.GetBytes(bitLength);
        if (!BitConverter.IsLittleEndian)
            Array.Reverse(lengthBytes);
        lengthBytes.CopyTo(paddedInput, paddedInput.Length - 8);

        return paddedInput;
    }

    private static uint RotateLeft(uint x, int n)
    {
        return (x << n) | (x >> (32 - n));
    }
}

使用方式

调用Md5Helper.ComputeMd5Hash方法传入邮箱地址,即可得到Gravatar所需的MD5哈希字符串:

string email = "user@example.com";
string gravatarHash = Md5Helper.ComputeMd5Hash(email);
// 后续可拼接Gravatar头像URL

这个纯C#实现完全适配Blazor环境,无需依赖任何原生加密API或第三方库,也不需要JS互操作,完美满足Gravatar场景的MD5哈希需求。

内容的提问来源于stack exchange,提问作者Luke Vo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 15:58:26