在 Blazor WASM 中手撸一个.NET MD5类

30 篇文章 4 订阅
3 篇文章 0 订阅

最近.net8 blazor auto大火, 我也玩了一下,发现ssr能用的代码 MD5 类在wasm是没法用的. 于是搜索了一下互联网,找到了一份代码,分享给大家.

我找到的帖子作者原话: 代码不是我的,但我确实稍微修改了它以使其与 System.Security.Cryptography.MD5 类更加一致。

public static class MD5
{
    public static byte[] ComputeHash(byte[] input)
    {
        uint num = 1732584193u;
        uint num2 = 4023233417u;
        uint num3 = 2562383102u;
        uint num4 = 271733878u;
        int num5 = (56 - (input.Length + 1) % 64) % 64;
        byte[] array = new byte[input.Length + 1 + num5 + 8];
        Array.Copy(input, array, input.Length);
        array[input.Length] = 128;
        Array.Copy(BitConverter.GetBytes(input.Length * 8), 0, array, array.Length - 8, 4);
        for (int i = 0; i < array.Length / 64; i++)
        {
            uint[] array2 = new uint[16];
            for (int j = 0; j < 16; j++)
            {
                array2[j] = BitConverter.ToUInt32(array, i * 64 + j * 4);
            }

            uint num6 = num;
            uint num7 = num2;
            uint num8 = num3;
            uint num9 = num4;
            uint num10 = 0u;
            uint num11 = 0u;
            uint num12 = 0u;
            while (true)
            {
                switch (num12)
                {
                    case 0u:
                    case 1u:
                    case 2u:
                    case 3u:
                    case 4u:
                    case 5u:
                    case 6u:
                    case 7u:
                    case 8u:
                    case 9u:
                    case 10u:
                    case 11u:
                    case 12u:
                    case 13u:
                    case 14u:
                    case 15u:
                        num10 = num7 & num8 | ~num7 & num9;
                        num11 = num12;
                        goto IL_0138;
                    case 16u:
                    case 17u:
                    case 18u:
                    case 19u:
                    case 20u:
                    case 21u:
                    case 22u:
                    case 23u:
                    case 24u:
                    case 25u:
                    case 26u:
                    case 27u:
                    case 28u:
                    case 29u:
                    case 30u:
                    case 31u:
                    case 32u:
                    case 33u:
                    case 34u:
                    case 35u:
                    case 36u:
                    case 37u:
                    case 38u:
                    case 39u:
                    case 40u:
                    case 41u:
                    case 42u:
                    case 43u:
                    case 44u:
                    case 45u:
                    case 46u:
                    case 47u:
                    case 48u:
                    case 49u:
                    case 50u:
                    case 51u:
                    case 52u:
                    case 53u:
                    case 54u:
                    case 55u:
                    case 56u:
                    case 57u:
                    case 58u:
                    case 59u:
                    case 60u:
                    case 61u:
                    case 62u:
                    case 63u:
                        if (num12 >= 16 && num12 <= 31)
                        {
                            num10 = num9 & num7 | ~num9 & num8;
                            num11 = (5 * num12 + 1) % 16u;
                        }
                        else if (num12 >= 32 && num12 <= 47)
                        {
                            num10 = num7 ^ num8 ^ num9;
                            num11 = (3 * num12 + 5) % 16u;
                        }
                        else if (num12 >= 48)
                        {
                            num10 = num8 ^ (num7 | ~num9);
                            num11 = 7 * num12 % 16u;
                        }

                        goto IL_0138;
                }

                break;
            IL_0138:
                uint num13 = num9;
                num9 = num8;
                num8 = num7;
                num7 += leftRotate(num6 + num10 + K[num12] + array2[num11], s[num12]);
                num6 = num13;
                num12++;
            }

            num += num6;
            num2 += num7;
            num3 += num8;
            num4 += num9;
        }
        var hashBytes = new byte[16];
        BitConverter.GetBytes(num).CopyTo(hashBytes, 0);
        BitConverter.GetBytes(num2).CopyTo(hashBytes, 4);
        BitConverter.GetBytes(num3).CopyTo(hashBytes, 8);
        BitConverter.GetBytes(num4).CopyTo(hashBytes, 12);
        return hashBytes;
    }
    public static string ComputeHashString(byte[] input) => string.Join("", ComputeHash(input).Select(o => o.ToString("x2")));

    private static int[] s = new int[64]
    {
        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
    };

    private static uint[] K = new uint[64]
    {
        3614090360u, 3905402710u, 606105819u, 3250441966u, 4118548399u, 1200080426u, 2821735955u, 4249261313u, 1770035416u, 2336552879u,
        4294925233u, 2304563134u, 1804603682u, 4254626195u, 2792965006u, 1236535329u, 4129170786u, 3225465664u, 643717713u, 3921069994u,
        3593408605u, 38016083u, 3634488961u, 3889429448u, 568446438u, 3275163606u, 4107603335u, 1163531501u, 2850285829u, 4243563512u,
        1735328473u, 2368359562u, 4294588738u, 2272392833u, 1839030562u, 4259657740u, 2763975236u, 1272893353u, 4139469664u, 3200236656u,
        681279174u, 3936430074u, 3572445317u, 76029189u, 3654602809u, 3873151461u, 530742520u, 3299628645u, 4096336452u, 1126891415u,
        2878612391u, 4237533241u, 1700485571u, 2399980690u, 4293915773u, 2240044497u, 1873313359u, 4264355552u, 2734768916u, 1309151649u,
        4149444226u, 3174756917u, 718787259u, 3951481745u
    };

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

不得不说,真香.

参考来源

https://stackoverflow.com/questions/76728300/any-net-md5-library-nuget-for-blazor-webassembly

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Blazor一个用C#构建Web应用程序的框架,因此它的登陆页面可以使用ASP.NET Identity或其他身份验证库来实现。 在Blazor应用程序中,可以使用Blazor Server或Blazor WebAssembly(WASM)来实现登陆页面。Blazor Server是将C#代码运行在服务器上,并使用SignalR协议来与客户端交互;Blazor WebAssembly是将C#代码编译为WebAssembly二进制文件,并直接在客户端运行。 无论使用哪种模式,通常都需要在登陆页面中实现以下功能: 1. 输入用户名和密码,并在点击“登录”按钮时进行身份验证。 2. 显示错误消息,如果输入的用户名或密码不正确。 3. 如果身份验证成功,则将用户重定向到应用程序的主页。 以下是一个基本的Blazor登陆页面示例: ```html @page "/login" <h1>Login</h1> @if (errorMessage != null) { <div class="alert alert-danger">@errorMessage</div> } <form> <div class="form-group"> <label for="username">Username:</label> <input type="text" class="form-control" id="username" @bind-value="username"> </div> <div class="form-group"> <label for="password">Password:</label> <input type="password" class="form-control" id="password" @bind-value="password"> </div> <button type="submit" class="btn btn-primary" @onclick="Login">Login</button> </form> @code { private string username; private string password; private string errorMessage; private void Login() { // TODO: perform authentication // if authentication fails, set errorMessage to display error message // if authentication succeeds, redirect to main page // NavigationManager.NavigateTo("/main"); } } ``` 在这个示例中,我们定义了一个包含输入框和“登录”按钮的表单。当用户点击“登录”按钮时,我们调用Login方法来执行身份验证。如果身份验证失败,则设置errorMessage以显示错误消息。如果身份验证成功,则使用NavigationManager将用户重定向到应用程序的主页。 请注意,这只是一个基本示例,实际的Blazor登陆页面可能需要包含更多的功能和安全措施,例如防止跨站点请求伪造(CSRF)攻击,使用SSL加密通信,使用复杂密码策略等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Densen2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值