How to use MD5 and SHA in C#
EDIT: To remove the ‘-’ in the hashes, replace your ‘return’ line with this:
Regex.Replace(BitConverter.ToString(hashedBytes), "-", "");
Hashing in C# is quite a bit different then languages like PHP. In PHP to hash a string via MD5 you would simply use the md5 function as so:
md5("string")
Let’s stop complaining and get right down to it.
Create a new Console project in either MS Visual C# or Mono. Make a new class after your main one. We’ll call it Hasher. Add the following to the top of your file.
using System.Security.Cryptography;
Now add the following code to your Hasher class
public string Hash(string originalString)
{
Byte[] originalBytes;
Byte[] hashedBytes;
System.Security.Cryptography.MD5 md5;
md5 = new MD5CryptoServiceProvider();
originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
encodedBytes = md5.ComputeHash(originalBytes);
return BitConverter.ToString(hashedBytes);
}
I won’t explain this in much detail. Mainly for the reason that it’s fairly simple and for the most part self explanatory.
Now we need to make the front end. Add the following to your Main class.
Console.Write("Enter your string: ");
string text = Console.ReadLine();
Hasher crypt = new Hasher();
string md5 = crypt.Hash(text);
Console.WriteLine("MD5: " + md5);
That takes the input it gets in the terminal, puts it in the Hash function and then writes the result (the MD5 hash) to the console. Pretty simple stuff. Now we’re gonna do this with SHA.
C# supports four different types of SHA. SHA1, SHA256, SHA384, and finally SHA512. Here is the entire code for converting text to SHA1.
using System;
using System.Text;
using System.Security.Cryptography;
namespace SHA1
{
class MainClass
{
public static void Main(string[] args)
{
Console.Write("Enter your string: ");
string text = Console.ReadLine();
Hasher crypt = new Hasher();
string sha1 = crypt.Hash(text);
Console.WriteLine("SHA1: " + sha1);
}
}
class Hasher
{
public string Hash(string originalString)
{
Byte[] originalBytes;
Byte[] hashedBytes;
System.Security.Cryptography.SHA1 sha1;
sha1 = new SHA1CryptoServiceProvider();
originalBytes = ASCIIEncoding.Default.GetBytes(originalString);
hashedBytes = sha1.ComputeHash(originalBytes);
return BitConverter.ToString(hashedBytes);
}
}
}
As you might have noticed it was largely a copy and paste of our MD5 hasher. The above script can also be modified to create different types of SHA hashes like those previously mentioned. Really, you can just switch out SHA1 with the other kind of SHA hash you want. For instance instead of System.Security.Cryptography.SHA1 sha1; you could put System.Security.Cryptography.SHA256 sha256; And then change a few other instances.
I sorta wish C# provided an easy(er) way to convert hashes on the fly but to my knowledge it does not. You can stick these in your application and use them to give you the functionality C# doesn’t give us on its own. By the way, if you get any errors with the stuff I posted, all of them should be easily fixed. Let me know of my errors so I can fix them!
Good luck!
