Common C# method to compute hash values using different hashing algorithms from BouncyCastle.Crypto

Gautam Mokal
2 min readAug 3, 2021

There are many different hashing algorithms like MD5,Sha1,Sha384, Sha512 etc. used for computing hash values for a string value. Hash values are useful in many applications like communications, bloom filters etc.

Recently I wrote a simple application where I wanted to have a method which would take a string value and a name of algorithm and return the hashed value.

I used ‘BouncyCastle Crypto’ package which helped me to write this common method. Here is the code which I wrote for the method GetHashedValue(string algorithmName, string plainText).

private string GetHashedText(string algorithm, string plainText)
{
var nameOfAlog = (algorithm.Contains("-")) ? algorithm.Split('-')[0] : algorithm;
var type = Type.GetType("Org.BouncyCastle.Crypto.Digests." + nameOfAlog.Trim() + "Digest,BouncyCastle.Crypto");
var constructor = type.GetType().GetConstructors().Any(c => c.GetParameters().Length == 0);
var cdf = type.GetConstructor(new Type[0]);
Org.BouncyCastle.Crypto.IDigest digest = null;
if (algorithm.Contains("-"))
{
digest = (IDigest)Activator.CreateInstance(type, Int32.Parse(algorithm.Split('-')[1]));
}
else
{
digest = (IDigest)Activator.CreateInstance(type);
}
var data = System.Text.Encoding.UTF8.GetBytes(plainText); digest.BlockUpdate(data, 0, data.Length);
byte[] result = new byte[digest.GetDigestSize()];
digest.DoFinal(result, 0);
string encrypted = Hex.ToHexString(result);
return encrypted;
}

The name of algorithm was used to create an instance of the Digest class which was available from in BouncyCastle.Crypto namespace.

If the name contains any “-”, for example algorithm names like Shah3–224 where second part of split i.e. 224 was used as a parameter to pass in the constructor of BouncyCastle.Crypto.Digests.<Algorithm>Digest class. <Algorithm> would be replaced by the name of the algorithm parameter.

This helped to create an instance of the digest was created with a default constructor or with 1 parameter constructor depending upon the algorithm name.

Once digest instance was created, it converted the plainText to hashed value.

For above code a NuGet Package ‘BouncyCastle.Crypto’ was added from NuGet package manger in Visual Studio 2019. The method works with most of hashing algorithms.

Following namespaces were added in my application.

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Utilities.Encoders;

My main method from which I called GetHashedText method was as follows:

static void Main(string[] args)
{
List<string> hashers = "MD2, Gost3411, MD4, MD5, RipeMD128, RipeMD160, RipeMD256, RipeMD320, Sha1, Sha224, Sha256, Sha384, Sha3-224,Sha3-256, Sha512, Sha512t-224, Tiger, Whirlpool".Split(',').ToList();
foreach (var hasher in hashers)
{
Console.WriteLine(hasher + " -> " +new Program().GetHashedText(hasher, "SomeText"));
} Console.ReadKey();
}

Following was the output after I ran my console application.

MD2 -> 7448e745a307fb4dbf9e9ca0aa392387 
Gost3411 -> e38c2f8b08f5164faf94cdd5aa01284f20972e60e0e56af90e7830fdf6240d8d
MD4 -> a54a3bdd334ee3c3cd33afaaf1ab019f
MD5 -> 5b6865e9622804fd70324543d06869c4
RipeMD128 -> 636b7c9ad2c5a742ac30e7d763a822b1
RipeMD160 -> 713509bbe1ca77015892ed4ec21e62a958fb2acf
RipeMD256 -> f133ab2afc7a32ca3de647f2ec0398d2cd1fc7eff687a7c6bc09a366f23296e7 RipeMD320 -> 7c064ce877b386fa8aedf8e6aea94e4bf7009316dbf15e36e6b305ed73bc6e377a087730e20938fc
Sha1 -> d1ee6f7709a0c939ad55168b93a0fa5377ed69ec
Sha224 -> 147aa66d56be9d5b41e01c1c6fd7119c7ca864e4a1736078ea4cd462 Sha256 -> 8ae020d34c5656eda3ec2270e4ba32021be4d706b8f63187944b3428cfd5c1ef Sha384 -> b3c0c3d4e653236f47459cb9789e5fefcddc66de1803807eca79c2c0f9ba05f5d6447d8a412ee71251c7ae010ae5dda4
Sha3-224 -> bc4f8dcc0fd7c9f6e9f9a0c3194b7e51b16de2714a40e9e8c0bbec03 Sha3-256 -> 18dc1f0f47f4661d94f6ab94d2c338da7fc0cd7aa0c6d3c1d8d2637ef2744ff9 Sha512 -> 50fb0b5ef56a624538b253ef9227b75c6742cda9d227dd49410c04e3e025c5133c8456b457b09f56a42304dab85ac4c1e1ad80190866607e9c3b8633bdc1440e Sha512t-224 -> e7054d182afd15f4f88c1c7cb797e6cbff194a470136561bb7bed41c
Tiger -> e79078c3835adba5a62cec7173b95023505bf701663246bb
Whirlpool -> a746f4a8ab822daa1c74b9c9c5d909b8971ae9be2008bc3fff74579ce0d0fb1bd3e15e536bfa29c35fd6c5a1ecd1cfe37c36b424220a4dc61b9267d4fa8aee1a

This method helped me to have a common method which saved duplicate code and time.

Hope this can help you too!

Gautam Mokal

--

--