The blog has moved to a new address. The blog is now located at http://devintelligence.com

Adsense

Sunday, December 02, 2007

SHA1 hash calculation in C#

The SHA1 algorithm can be used to calculate check sum, that very useful when you want to ensure the string is not broken .Also for simple  password verification.The example below show how to calculate SHA1 for given string .

 

/// <summary>
/// Calculates SHA1 hash
/// </summary>
/// <param name="text">input string</param>
/// <param name="enc">Character encoding</param>
/// <returns>SHA1 hash</returns>
public static string CalculateSHA1(string text, Encoding enc)
{
byte[] buffer = enc.GetBytes(text);
SHA1CryptoServiceProvider cryptoTransformSHA1 =
new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(
cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");

return hash;
}


The result SHA1 hash for the string "first string":



image



Technorati Tags: ,,,