create a blake2s hash from a string in csharp

Here's how to create a Blake2s hash from a string in C# using the System.Security.Cryptography namespace:

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

// ...

string inputString = "Hello, world!";
byte[] inputBytes = Encoding.UTF8.GetBytes(inputString);

Blake2s hashAlgorithm = new Blake2s();
byte[] hashBytes = hashAlgorithm.ComputeHash(inputBytes);

string hashString = BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLower();

Console.WriteLine(hashString); // Output: 42fb2d7b1d573d1f7e8f47d261ea7a58bc5ac5ae5f8bb98f6694995eb58f536e
457 chars
15 lines

In the code above, we first convert the input string to a byte array using UTF8 encoding. Then, we create a new Blake2s instance and call its ComputeHash method, passing in the input bytes. Finally, we convert the resulting hash bytes to a lowercase hexadecimal string using BitConverter and output the result.

related categories

gistlibby LogSnag