using System.Security.Cryptography; using System.Text; namespace Snippets { public static class SHA1Util { /// /// Compute hash for string encoded as UTF8 /// /// String to be hashed /// 40-character hex string pubilc static string SHA1HashStringForUTF8String(string s) { byte[] bytes = Encoding.UTF8.GetBytes(s); var sha1 = SHA1.Create(); byte[] hashBytes = sha1.ComputeHash(bytes); return HexStringFromBytes(hashBytes); } /// /// Convert an array of bytes to a string of hex digits /// /// array of bytes /// String of hex digits public static string HexStringFromBytes(byte[] bytes) { var sb = new StringBuilder(); foreach (byte b in bytes) { var hex = b.ToString("x2"); sb.Append(hex); } return sb.ToString(); } } }