Bouncy Castle implements a number of hashing hashing digests, including GOST, Whirlpool, Tiger192 and RipeMD160 (as used in Bitcoin). In this case we will use Bouncy Castle and C#, and implement a range of methods, including modern hashing methods such as Ascon, Blake2b and Blake.
Hashing using Bouncy Castle and C# |
Code
We can create a Dotnet console project for .NET 8.0 with:
dotnet new console --framework net8.0
First we install the Bouncy Castle library:
dotnet add package BouncyCastle.Cryptography
Next some code:
namespace AES { using System.Security.Cryptography; using Org.BouncyCastle.Crypto.Digests; class Program { static void Main(string[] args) { string str=""; if (args.Length >0) str=args[0]; try { Console.WriteLine ("Message:\t{0}\n",str); var h2 = new KeccakDigest(); h2.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash2=new byte[h2.GetDigestSize()]; h2.DoFinal(hash2, 0); Console.WriteLine ("Keccak:\t\t{0}",Convert.ToHexString(hash2)); var h3 = new MD2Digest(); h3.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash3=new byte[h3.GetDigestSize()]; h3.DoFinal(hash3, 0); Console.WriteLine ("MD2:\t\t{0}",Convert.ToHexString(hash3)); var h4 = new MD4Digest(); h4.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash4=new byte[h4.GetDigestSize()]; h4.DoFinal(hash4, 0); Console.WriteLine ("MD4:\t\t{0}",Convert.ToHexString(hash4)); var h5 = new RipeMD128Digest(); h5.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash5=new byte[h5.GetDigestSize()]; h5.DoFinal(hash5, 0); Console.WriteLine ("RipeMD128:\t{0}",Convert.ToHexString(hash5)); var h6 = new RipeMD160Digest(); h6.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash6=new byte[h6.GetDigestSize()]; h6.DoFinal(hash6, 0); Console.WriteLine ("RipeMD160:\t{0}",Convert.ToHexString(hash6)); var h7 = new RipeMD256Digest(); h7.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash7=new byte[h7.GetDigestSize()]; h7.DoFinal(hash7, 0); Console.WriteLine ("RipeMD256:\t{0}",Convert.ToHexString(hash7)); var h8 = new RipeMD320Digest(); h8.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash8=new byte[h8.GetDigestSize()]; h8.DoFinal(hash8, 0); Console.WriteLine ("RipeMD320:\t{0}",Convert.ToHexString(hash8)); var h9 = new TigerDigest(); h9.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash9=new byte[h9.GetDigestSize()]; h9.DoFinal(hash9, 0); Console.WriteLine ("Tiger192:\t{0}",Convert.ToHexString(hash9)); var h10 = new WhirlpoolDigest(); h10.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash10=new byte[h10.GetDigestSize()]; h10.DoFinal(hash10, 0); Console.WriteLine ("Whirlpool:\t{0}",Convert.ToHexString(hash10)); var h11 = new MD5Digest(); h11.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash11=new byte[h11.GetDigestSize()]; h11.DoFinal(hash11, 0); Console.WriteLine ("MD5:\t\t{0}",Convert.ToHexString(hash11)); var h12 = new Sha1Digest(); h12.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash12=new byte[h12.GetDigestSize()]; h12.DoFinal(hash12, 0); Console.WriteLine ("SHA1:\t\t{0}",Convert.ToHexString(hash12)); var h13 = new Sha256Digest(); h13.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash13=new byte[h13.GetDigestSize()]; h13.DoFinal(hash13, 0); Console.WriteLine ("SHA256:\t\t{0}",Convert.ToHexString(hash13)); var h14 = new Sha3Digest(); h14.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash14=new byte[h14.GetDigestSize()]; h14.DoFinal(hash14, 0); Console.WriteLine ("SHA3:\t\t{0}",Convert.ToHexString(hash14)); var h15 = new Sha384Digest(); h15.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash15=new byte[h15.GetDigestSize()]; h15.DoFinal(hash15, 0); Console.WriteLine ("SHA384:\t\t{0}",Convert.ToHexString(hash15)); var h16 = new Blake3Digest(); h16.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash16=new byte[h16.GetDigestSize()]; h16.DoFinal(hash16, 0); Console.WriteLine ("Blake 3:\t{0}",Convert.ToHexString(hash16)); var h17 = new Blake2bDigest(); h17.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash17=new byte[h17.GetDigestSize()]; h17.DoFinal(hash17, 0); Console.WriteLine ("Blake2b:\t{0}",Convert.ToHexString(hash17)); var h24 = new CShakeDigest(128,null,null); h24.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash24=new byte[h24.GetDigestSize()]; h24.DoFinal(hash24, 0); Console.WriteLine ("CSHAKE128:\t{0}",Convert.ToHexString(hash24)); var h25 = new CShakeDigest(256,null,null); h25.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash25=new byte[h25.GetDigestSize()]; h25.DoFinal(hash25, 0); Console.WriteLine ("CSHAKE256:\t{0}",Convert.ToHexString(hash25)); Console.WriteLine ("=== Ukrainian hashes ==="); var h26 = new Dstu7564Digest(256); h26.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash26=new byte[h26.GetDigestSize()]; h26.DoFinal(hash26, 0); Console.WriteLine ("Dstu7564:\t{0}",Convert.ToHexString(hash26)); Console.WriteLine ("=== Russian hashes ==="); var h = new Gost3411Digest(); h.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash=new byte[h.GetDigestSize()]; h.DoFinal(hash, 0); Console.WriteLine ("GOST:\t\t{0}",Convert.ToHexString(hash)); Console.WriteLine ("=== Chinese hashes ==="); var h19 = new SM3Digest(); h19.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash19=new byte[h19.GetDigestSize()]; h19.DoFinal(hash19, 0); Console.WriteLine ("SM3:\t\t{0}",Convert.ToHexString(hash19)); Console.WriteLine ("=== Light-weight hashes ==="); var h20 = new SparkleDigest(new SparkleDigest.SparkleParameters()); h20.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash20=new byte[h20.GetDigestSize()]; h20.DoFinal(hash20, 0); Console.WriteLine ("Sparkle:\t{0}",Convert.ToHexString(hash20)); var h18 = new AsconDigest(new AsconDigest.AsconParameters()); h18.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash18=new byte[h18.GetDigestSize()]; h18.DoFinal(hash18, 0); Console.WriteLine ("Ascon:\t\t{0}",Convert.ToHexString(hash18)); var h21 = new XoodyakDigest(); h21.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash21=new byte[h21.GetDigestSize()]; h21.DoFinal(hash21, 0); Console.WriteLine ("Xoodyak:\t{0}",Convert.ToHexString(hash21)); var h22 = new PhotonBeetleDigest(); h22.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash22=new byte[h22.GetDigestSize()]; h22.DoFinal(hash22, 0); Console.WriteLine ("PhotonBeetle:\t{0}",Convert.ToHexString(hash22)); var h23 = new IsapDigest(); h23.BlockUpdate(System.Text.Encoding.UTF8.GetBytes(str), 0, str.Length); var hash23=new byte[h23.GetDigestSize()]; h22.DoFinal(hash23, 0); Console.WriteLine ("ISAP:\t\t{0}",Convert.ToHexString(hash23)); } catch (Exception e) { Console.WriteLine("Error: {0}",e.Message); } } } }
A sample run showing the cipher text, and the public and private key is:
Message: The quick brown fox jumps over the lazy dog Keccak: 0BBE6AFAE0D7E89054085C1CC47B1689772C89A41796891E197D1CA1B76F288154933DED MD2: 03D85A0D629D2C442E987525319FC471 MD4: 1BEE69A46BA811185C194762ABAEAE90 RipeMD128: 3FA9B57F053C053FBE2735B2380DB596 RipeMD160: 37F332F68DB77BD9D7EDD4969571AD671CF9DD3B RipeMD256: C3B0C2F764AC6D576A6C430FB61A6F2255B4FA833E094B1BA8C1E29B6353036F RipeMD320: E7660E67549435C62141E51C9AB1DCC3B1EE9F65C0B3E561AE8F58C5DBA3D21997781CD1CC6FBC34 Tiger192: 6D12A41E72E644F017B6F0E2F7B44C6285F06DD5D2C5B075 Whirlpool: B97DE512E91E3828B40D2B0FDCE9CEB3C4A71F9BEA8D88E75C4FA854DF36725FD2B52EB6544EDCACD6F8BEDDFEA403CB55AE31F03AD62A5EF54E42EE82C3FB35 MD5: 9E107D9D372BB6826BD81D3542A419D6 SHA1: 2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12 SHA256: D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592 SHA3: 69070DDA01975C8C120C3AADA1B282394E7F032FA9CF32F4CB2259A0897DFC04 SHA384: CA737F1014A48F4C0B6DD43CB177B0AFD9E5169367544C494011E3317DBF9A509CB1E5DC1E85A941BBEE3D7F2AFBC9B1 Blake 3: 2F1514181AADCCD913ABD94CFA592701A5686AB23F8DF1DFF1B74710FEBC6D4A Blake2b: A8ADD4BDDDFD93E4877D2746E62817B116364A1FA7BC148D95090BC7333B3673F82401CF7AA2E4CB1ECD90296E3F14CB5413F8ED77BE73045B13914CDCD6A918 CSHAKE128: F4202E3C5852F9182A0430FD8144F0A74B95E7417ECAE17DB0F8CFEED0E3E66E CSHAKE256: 2F671343D9B2E1604DC9DCF0753E5FE15C7C64A0D283CBBF722D411A0E36F6CA1D01D1369A23539CD80F7C054B6E5DAF9C962CAD5B8ED5BD11998B40D5734442 === Ukrainian hashes === Dstu7564: 996899F2D7422CEAF552475036B2DC120607EFF538ABF2B8DFF471A98A4740C6 === Russian hashes === GOST: 9004294A361A508C586FE53D1F1B02746765E71B765472786E4770D565830A76 === Chinese hashes === SM3: 5FDFE814B8573CA021983970FC79B2218C9570369B4859684E2E4C3FC76CB8EA === Light-weight hashes === Sparkle: D43F87A0FE60FC5925064880C6116C136B6D94FA24A93DFFCB35D178C3AF932C Ascon: 3375FB43372C49CBD48AC5BB6774E7CF5702F537B2CF854628EDAE1BD280059E Xoodyak: 087376B970C53ED0339A4FE54F4462F0F34E4E50ED09B4314ED24B32BA9822CB PhotonBeetle: 5CED20C8D747C62114BF691739821516135AA8413997CF34B4B8E40A25489762 ISAP: 24C50C761A324E46957C77FA52BB7D135A05C808BEAFED1BA6FEE32EC8A082D0