Subscribe Us

LightBlog

Tuesday, 14 December 2021

Hashing with SHA1 Algorithm in C#


For those who want a "standard" text formatting of the hash, you can use something like the following:
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();
    }
}

No comments:

Post a Comment