How to generate the SHA-1 hash of a string using C#

The namespace System.Security.Cryptography contains classes for working with cryptography such as hashing. Within this namespace you will find the SHA1Managed class which we can utilize to generate the SHA-1 hash of a string. Below you will find a helper method using this class:


public static string GetSha1(string value)
{
	var data = Encoding.ASCII.GetBytes(value);
	var hashData = new SHA1Managed().ComputeHash(data);

	var hash = string.Empty;

	foreach (var b in hashData)
		hash += b.ToString("X2");

	return hash;
}
Published: 24.03.2009
blog comments powered by Disqus