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: ,,,

10 comments:

Anonymous said...

very good and compact code

GraffitiKnight said...
This comment has been removed by the author.
Thiago Lechuga said...

Yeah, O_o He could do like this to:

public string getSHA1(string userPassword){return BitConverter.ToString(SHA1Managed.Create().ComputeHash(Encoding.Default.GetBytes(userPassword))).Replace("-", "");}

I think that being Legible is more important.

GraffitiKnight said...
This comment has been removed by the author.
Taras said...

to:Graffiti Knight
I don't think when you connect all methods calls that no extra objects will be created .Your example is less maintainable.

GraffitiKnight said...
This comment has been removed by the author.
Taras said...

Why not ? Take look at two examples below.
string s = " 111:222:333:111 ".Replace( "111", string.Empty ).Replace( ":", string.Empty ).Trim(' ');

================================

Now, more clear(from my point of view) example
string s = " 111:222:333:111 ";
s = s.Replace( "111", string.Empty );
s = s.Replace( ":", string.Empty );
s = s.Trim(' ');

I prefer to use a code style from the last example.I can easily navigate between lines.Make modifications .In the first example
- it depends how long the code statement is.

Anonymous said...

Just as an FYI...

If what you need is FIPS 140-2 compliant code, you will need to use SHA1CryptoServiceProvider and not SHA1Managed.

My 2cents:

As for writing compact code, it's subjective. Working in a corporate environment where at any given time I'm asked to modify legacy and/or code written by others. I agree with Taras, as I have found modifying code that is easily navigated saves me bundles of time.

Even though I began coding like Graffiti Knight, trying to recreate the world in a single line of code, I have reverted to a writing style that lends itself to allowing others the ability to pickup and manage without calling me at 1am for support.

evilripper said...

thanks very much!!
bye

Anonymous said...

Graffiti Knight:

By saying "My example doesn't create extra objects that then need to be garbage collected", you prove that you have absolutely no idea what's going on behind the scenes. Just because you haven't explicitly assigned the objects to variables, it doesn't mean they're somehow not in memory and don't need to be garbage collected.

Because you think otherwise, you think that putting all your code on one line to somehow save memory is a good idea. In fact it's a terrible idea: it's not maintainable, it's hard to read, it's hard to debug and it's hard to insert say, error checking in intermediate steps.

To top it off, you rant about your code being better and cite rookie misconceptions to people who know better than you!

Since you posted about 2 years ago, I hope you have since learnt better.