CryptoSys PKI Pro Manual

Example: Hmac.HexFromBytes 

[VB6 equivalent: HMAC_HexFromBytes]

Dim strData As String
Dim strKey As String
Dim abData() As Byte
Dim abKey() As Byte
Dim strDigest As String

' Test case 2 from RFC 2202 and RFC 4231
strData = "what do ya want for nothing?"
strKey = "Jefe"

' Convert message and key into Byte format
abData = System.Text.Encoding.Default.GetBytes(strData)
abKey = System.Text.Encoding.Default.GetBytes(strKey)

' Compute default HMAC (HMAC-SHA-1)
strDigest = Hmac.HexFromBytes(abData, abKey, HashAlgorithm.Sha1)
If strDigest.Length = 0 Then Exit Sub ' ERROR
Console.WriteLine("HMAC-SHA-1  =" & strDigest)
Console.WriteLine("CORRECT     =" & "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")

' Compute HMAC-MD5
strDigest = Hmac.HexFromBytes(abData, abKey, HashAlgorithm.Md5)
If strDigest.Length = 0 Then Exit Sub ' ERROR
Console.WriteLine("HMAC-MD5    =" & strDigest)
Console.WriteLine("CORRECT     =" & "750c783e6ab0b503eaa86e310a5db738")

' Compute HMAC-SHA-256
strDigest = Hmac.HexFromBytes(abData, abKey, HashAlgorithm.Sha256)
If strDigest.Length = 0 Then Exit Sub ' ERROR
Console.WriteLine("HMAC-SHA-256=" & strDigest)
Console.WriteLine("CORRECT     =" & "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843")

' Compute HMAC-SHA-512
strDigest = Hmac.HexFromBytes(abData, abKey, HashAlgorithm.Sha512)
If strDigest.Length = 0 Then Exit Sub ' ERROR
Console.WriteLine("HMAC-SHA-512=" & strDigest)
Console.WriteLine("CORRECT     =" _
& "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" _
& "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737")

See Also:
Hmac.HexFromBytes Method

[Contents] [Index]

[PREV: Example: Hmac.Bytes ...]   [Contents]   [Index]   
   [NEXT: Example: Hmac.HexFromHex ...]

Copyright © 2004-23 D.I. Management Services Pty Ltd. All rights reserved. Generated 2023-09-18T10:02:53Z.