Hash digest

Hash digests can be created with the DidiSoft.OpenSsl.OpenSslDigest class which provides static Digest methods that accept data of type string, Stream, FileInfo and byte array.

The available Hash algorithms are listed in the DidiSoft.OpenSsl.HashAlgorithm enum. Here you can see how to use them:

Hash of a string

C# example

byte[] digest = OpenSslDigest.Digest(HashAlgorithm.Sha1, "Hello World");
Console.WriteLine(OpenSslUtil.ToHexString(digest));

VB.NET example

Dim digest As Byte() = OpenSslDigest.Digest(HashAlgorithm.Sha1, "Hello World")
Console.WriteLine(OpenSslUtil.ToHexString(digest))

Hash of a File

C# example

byte[] digest = OpenSslDigest.Digest(HashAlgorithm.Sha1, new FileInfo("myfile.dat"));
Console.WriteLine(OpenSslUtil.ToHexString(digest));

VB.NET example

Dim digest As Byte() = OpenSslDigest.Digest(HashAlgorithm.Sha1, New FileInfo("myfile.dat"))
Console.WriteLine(OpenSslUtil.ToHexString(digest))

Hash of a Stream

C# example

using (Stream s = File.OpenRead("myfile.dat"))
{
  byte[] digest = OpenSslDigest.Digest(HashAlgorithm.Sha1, s);
  Console.WriteLine(OpenSslUtil.ToHexString(digest));
}

VB.NET example

Using s As Stream = File.OpenRead("myfile.dat")
  Dim digest As Byte() = OpenSslDigest.Digest(HashAlgorithm.Sha1, New FileInfo("myfile.dat"))
  Console.WriteLine(OpenSslUtil.ToHexString(digest))
End Using

Hash of a byte array

C# example

byte[] data = new byte[] { 1, 2, 3, 4, 5 };
byte[] digest = OpenSslDigest.Digest(HashAlgorithm.Sha1, data);
Console.WriteLine(OpenSslUtil.ToHexString(digest));

VB.NET example

Dim input As Byte() = New Byte() {1, 2, 3, 4, 5}
Dim digest As Byte() = OpenSslDigest.Digest(HashAlgorithm.Sha1, input)
Console.WriteLine(OpenSslUtil.ToHexString(digest))

Summary

This tutorial chapter discussed how to create hash digest in similar to OpenSSL way. Created this way digests can be signed later with the OpenSSL RSA class.