create a blake2b hash from a string in csharp

To create a Blake2b hash from a string in C#, you can use the Blake2b class from the System.Security.Cryptography namespace. Here's an example code snippet:

main.cs
using System;
using System.Security.Cryptography;
using System.Text;

public static string GetBlake2bHash(string input)
{
    byte[] inputBytes = Encoding.UTF8.GetBytes(input);
    byte[] hashBytes;

    using (var blake2b = new Blake2bCryptoServiceProvider())
    {
        hashBytes = blake2b.ComputeHash(inputBytes);
    }

    return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
}
401 chars
17 lines

In this example, the GetBlake2bHash method takes a string input and returns a string representation of the hash. The input string is first converted to a byte array using the UTF8 encoding. Then, a new instance of the Blake2bCryptoServiceProvider class is created and used to compute the hash bytes. Finally, the hash bytes are converted to a string representation using the BitConverter class.

Note that this code requires .NET Framework 4.7.2 or later, or .NET Core 2.1 or later, since the Blake2bCryptoServiceProvider class is not available in earlier versions.

gistlibby LogSnag