For those who want a "standard" text formatting of the hash, you can use something like the following for .NET 5 and newer:
static string Hash(string input)
=> Convert.ToHexString(SHA1.HashData(Encoding.UTF8.GetBytes(input)));
This will produce a hash like 0C2E99D0949684278C30B9369B82638E1CEAD415.
If you are stuck on pre-.NET 5 where Convert.ToHexString is not available, you can do:
static string Hash(string input)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
var sb = new StringBuilder(hash.Length * 2);
foreach (byte b in hash)
{
// can be "x2" if you want lowercase
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
}
Or for a code golfed .Net Framework version:
static string Hash(string input)
{
var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input));
return string.Concat(hash.Select(b => b.ToString("x2")));
}
hex(e) == hex(E)