Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProIntegritate/e2a8ff0a198d5de40cddf1bfab43b4c4 to your computer and use it in GitHub Desktop.
Save ProIntegritate/e2a8ff0a198d5de40cddf1bfab43b4c4 to your computer and use it in GitHub Desktop.
VB.Net Bouncycastle Digest Example for SHA3 (Keccak)
Imports Org.BouncyCastle.Crypto.Digests
Sub main()
' SHA3 Test vectors for string 'abc'
' SHA-3-224 e642824c3f8cf24a d09234ee7d3c766f c9a3a5168d0c94ad 73b46fdf
' SHA-3-256 3a985da74fe225b2 045c172d6bd390bd 855f086e3e9d525b 46bfe24511431532
' SHA-3-384 ec01498288516fc9 26459f58e2c6ad8d f9b473cb0fc08c25 96da7cf0e49be4b2 98d88cea927ac7f5 39f1edf228376d25
' SHA-3-512 b751850b1a57168a 5693cd924b6b096e 08f621827444f70d 884f5d0240d2712e 10e116e9192af3c9 1a7ec57647e39340 57340b4cf408d5a5 6592f8274eec53f0
Dim bContent() As Byte = System.Text.Encoding.Default.GetBytes("abc")
Console.WriteLine("SHA3 (Keccak) testvectors for string 'abc':" & vbCrLf)
Dim SHA3_224 As New Sha3Digest(224)
Dim SHA3_256 As New Sha3Digest(256)
Dim SHA3_384 As New Sha3Digest(384)
Dim SHA3_512 As New Sha3Digest(512)
' ---- Length: 224 ----
SHA3_224.BlockUpdate(bContent, 0, bContent.Length)
Dim bHash(27) As Byte
SHA3_224.DoFinal(bHash, 0)
Console.Write("Keccak-224 = ") : Dump(bHash)
' ---- Length: 256 ----
SHA3_256.BlockUpdate(bContent, 0, bContent.Length)
ReDim bHash(31)
SHA3_256.DoFinal(bHash, 0)
Console.Write("Keccak-256 = ") : Dump(bHash)
' ---- Length: 384 ----
SHA3_384.BlockUpdate(bContent, 0, bContent.Length)
ReDim bHash(47)
SHA3_384.DoFinal(bHash, 0)
Console.Write("Keccak-384 = ") : Dump(bHash)
' ---- Length: 512 ----
SHA3_512.BlockUpdate(bContent, 0, bContent.Length)
ReDim bHash(63) 'As Byte ' 32 = 256, 64 = 512
SHA3_512.DoFinal(bHash, 0)
Console.Write("Keccak-512 = ") : Dump(bHash)
End Sub
Sub Dump(ByVal bBytes() As Byte)
For n = 0 To UBound(bBytes)
Console.Write(Microsoft.VisualBasic.Right("00" & Hex(bBytes(n)), 2))
If (n > 0) And ((n + 1) Mod 8 = 0) Then Console.Write(" ")
Next
Console.Write(vbCrLf & vbCrLf)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment