Calculate SHA1 hash from String in C#

Calculate SHA1 hash with this function.

public String SHA1(String plaintext)
{
	try
	{
		System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
		byte[] bytes = System.Text.Encoding.ASCII.GetBytes(plaintext);
		byte[] hash = sha1.ComputeHash(bytes);
 
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < hash.Length; i++)
			sb.Append(hash[i].ToString("x2"));
 
		return sb.ToString();
	}
	catch
	{
		return null;
	}
}

enjoy!

Categories: C# Tags: , , , ,
%d bloggers like this: