Search code examples
c#blazorblazor-webassembly

How can I use MD5 in Blazor WASM when it is deprecated?


In a .NET 8 console app, this code works:

using var hasher = MD5.Create();
var bytes = Encoding.UTF8.GetBytes("Hello world!");
var hashBytes = hasher.ComputeHash(bytes);
var hashHex = Convert.ToHexString(hashBytes);
Console.WriteLine(hashHex);
// Prints: 86FB269D190D2C85F6E0468CECA42A20

In Blazor WASM I get this error:

Error: One or more errors occurred. ('MD5' is not a known hash algorithm)

I found out MD5 is not recommended anymore but it is still used by the software library I need to work with. Any ideas? I am open to a Javascript MD5 library if required.


Solution

  • I have answered a similar question that answers this question here: Any .NET MD5 library (Nuget) for Blazor (WebAssembly)?

    The code in that post is from the open source library Radzen. I modified it slightly to more align with the original MS implementation.

    Example use:

    var bytes = Encoding.UTF8.GetBytes("Hello world!");
    var hashBytes = MD5.ComputeHash(bytes);
    var hashHex = Convert.ToHexString(hashBytes);
    Console.WriteLine(hashHex);
    // 86FB269D190D2C85F6E0468CECA42A20