Use RIPEMD160 with .NET Core! with Bouncy Castle
A note on implementing the hash algorithm RIPEMD160 in C # using Bouncy Castle.
This algorithm is used for Bitcoin etc.!
environment
- .NET Core 3.1
RIPEMD160 disappeared
As you can see from the Official Documentation, the .NET Framework has the RIPEMD160 algorithm. It was officially supported, but was removed in .NET Core.
See Repository Issue (interpreted in my poor English)
Does that mean “algorithms not provided on the OS side will not be implemented in .NET Core!”?
I am troubled.
Use Bouncy Castle
Bouncy Castle is one of the encryption libraries. Java and C # are supported as languages.
It can also be used with .NET Core.
Let’s get in quickly from Nuget.
At the time of writing (August 24, 2020), it seems that the version is not the latest, so if you are interested, please go to Release in the repository.
How to use
Use the RipeMD160Digest class in the Org.BouncyCastle.Crypto.Digests namespace.
The code itself will be simple.
class Program
{
static void Main(string[] args)
{
var data = Encoding.UTF8.GetBytes("abcdefg");
var digest = new RipeMD160Digest();
var result = new byte[digest.GetDigestSize()];
digest.BlockUpdate(data, 0, data.Length);
digest.DoFinal(result, 0);
WriteBytes(result);
//874f9960c5d2b7a9b5fad383e1ba44719ebb743a
}
private static void WriteBytes(IEnumerable<byte> bytes)
{
Console.WriteLine(string.Join("", bytes.Select(x => $"{x:x2}")));
}
}
- Create an instance of RIPEMD160 and
- Call BlockUpdate and
- Get results with DoFinal.
It is a simple usage.
(Since the RipeMD160 specification is 20 bytes long, the value of digest.GetDigestSize ()
is also fixed at 20.)
Referenced
RipeMD160Digest.cs
GeneralDigest.cs
Bouncy Castle doesn’t have documentation for C #, so you’ll have to read the source code to learn it!
I hope my article helps you.