The Hash Class

I’ve been working on a C# Library to make hashing strings an easy (and fun?) task. I’ll show you the very basic bare-bones usage here:

Hashing mudkipz = new Hashing();
string m5 = mudkipz.md5("So, I herd u liek mudkipz.");
string s1 = mudkipz.sha1("So, I herd u liek mudkipz.");
string s256 = mudkipz.sha256("So, I herd u liek mudkipz.");
string s384 = mudkipz.sha384("So, I herd u liek mudkipz.");
string s512 = mudkipz.sha512("So, I herd u liek mudkipz.");

The library currently supports md5, sha1, sha256, sha384, and sha512. So yeah that’s all nice and easy to use and such but it there more? Yes, there is! There are four different formats your string can be hashed into:

  1. AB-CD-EF (default)
  2. ab-cd-ef
  3. ABCDEF
  4. abcdef

For any of you that have used PHP’s hashing functions you’ll notice that format number 4 is identical. Let’s say you want to have everything be formatted like PHP does. Using format 4.

Hashing mudkipz = new Hashing();
mudkipz.setFormat(4);
string blah1 = mudkipz.md5("So, I herd u liek mudkipz.");
string blah2 = mudkipz.sha1("So, I herd u liek mudkipz.");

You’ll notice that both strings will be formatted in the same way PHP would. We can switch back in the middle too.

Hashing mudkipz = new Hashing();
mudkipz.setFormat(4);
string blah1 = mudkipz.md5("So, I herd u liek mudkipz.");
string blah2 = mudkipz.sha1("So, I herd u liek mudkipz.");
mudkipz.setFormat(1);
string blah3 = mudkipz.md5("So, I herd u liek mudkipz.");
string blah4 = mudkipz.sha1("So, I herd u liek mudkipz.");

What if you want everything to be formatted with format 3 except for just one? Ya know, the odd man out. Instead of switching default formats you can just specify a extra format parameter.

Hashing mudkipz = new Hashing();
mudkipz.setFormat(3);
string blah1 = mudkipz.md5("So, I herd u liek mudkipz.");
string blah2 = mudkipz.sha1("So, I herd u liek mudkipz.", 4);
string blah3 = mudkipz.sha256("So, I herd u liek mudkipz.");

blah2 would then be formatted using format 4. All the others would use format 3.

That’s just about it. It’s licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. A link back would be nice, but not necessary. ;) Use the following links to download. Enjoy!

Complete Hashing Class Source (26)
Hashing DLL (27)

This entry was posted on Monday, November 9th, 2009 at 6:35 pm and is filed under Microsoft Visual C# .NET, Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

 

NO-WWW