Creates a keyed-hash based message authentication code (HMAC) in hexadecimal format from byte data.
VB6/VBA
Debug.Print "Testing MAC_HexFromBytes ..." Dim strData As String Dim strKey As String Dim abData() As Byte Dim abKey() As Byte Dim nDataLen As Long Dim nKeyLen As Long Dim strDigest As String Dim nRet As Long ' 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 = StrConv(strData, vbFromUnicode) abKey = StrConv(strKey, vbFromUnicode) nDataLen = UBound(abData) - LBound(abData) + 1 nKeyLen = UBound(abKey) - LBound(abKey) + 1 ' Dimension the output string to receive the digest strDigest = String(API_MAX_HASH_CHARS, " ") ' Compute default HMAC (HMAC-SHA-1) nRet = MAC_HexFromBytes(strDigest, Len(strDigest), abData(0), nDataLen, abKey(0), nKeyLen, 0) If nRet <= 0 Then Exit Sub ' ERROR strDigest = Left(strDigest, nRet) Debug.Print "HMAC-SHA-1 =" & strDigest Debug.Print "CORRECT =" & "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79" ' Compute HMAC-MD5 strDigest = String(API_MAX_HASH_CHARS, " ") nRet = MAC_HexFromBytes(strDigest, Len(strDigest), abData(0), nDataLen, abKey(0), nKeyLen, API_HASH_MD5) If nRet <= 0 Then Exit Sub ' ERROR strDigest = Left(strDigest, nRet) Debug.Print "HMAC-MD5 =" & strDigest Debug.Print "CORRECT =" & "750c783e6ab0b503eaa86e310a5db738" ' Compute HMAC-SHA-256 strDigest = String(API_MAX_HASH_CHARS, " ") nRet = MAC_HexFromBytes(strDigest, Len(strDigest), abData(0), nDataLen, abKey(0), nKeyLen, API_HASH_SHA256) If nRet <= 0 Then Exit Sub ' ERROR strDigest = Left(strDigest, nRet) Debug.Print "HMAC-SHA-256=" & strDigest Debug.Print "CORRECT =" & "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" ' Compute HMAC-SHA-512 strDigest = String(API_MAX_HASH_CHARS, " ") nRet = MAC_HexFromBytes(strDigest, Len(strDigest), abData(0), nDataLen, abKey(0), nKeyLen, API_HASH_SHA512) If nRet <= 0 Then Exit Sub ' ERROR strDigest = Left(strDigest, nRet) Debug.Print "HMAC-SHA-512=" & strDigest Debug.Print "CORRECT =" _ & "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" _ & "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737" _
Output
Testing MAC_HexFromBytes ... HMAC-SHA-1 =effcdf6ae5eb2fa2d27416d5f184df9c259a7c79 CORRECT =effcdf6ae5eb2fa2d27416d5f184df9c259a7c79 HMAC-MD5 =750c783e6ab0b503eaa86e310a5db738 CORRECT =750c783e6ab0b503eaa86e310a5db738 HMAC-SHA-256=5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843 CORRECT =5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843 HMAC-SHA-512=164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737 CORRECT =164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737
VB.NET
Console.WriteLine("Testing MAC_HexFromBytes ...") Dim strData As String Dim strKey As String Dim abData() As Byte Dim abKey() As Byte ''Dim nDataLen As Integer ''Dim nKeyLen As Integer Dim strDigest As String ''Dim nRet As Integer ' 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) ''nDataLen = UBound(abData) - LBound(abData) + 1 ''nKeyLen = UBound(abKey) - LBound(abKey) + 1 ' Dimension the output string to receive the digest ''strDigest = String(API_MAX_HASH_CHARS, " ") ' Compute default HMAC (HMAC-SHA-1) strDigest = Mac.HexFromBytes(abData, abKey, MacAlgorithm.HmacSha1) If strDigest.Length <= 0 Then Exit Sub ' ERROR ''strDigest = Left(strDigest, nRet) Console.WriteLine("HMAC-SHA-1 =" & strDigest) Console.WriteLine("CORRECT =" & "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") ' Compute HMAC-MD5 ''strDigest = String(API_MAX_HASH_CHARS, " ") strDigest = Mac.HexFromBytes(abData, abKey, MacAlgorithm.HmacMd5) If strDigest.Length <= 0 Then Exit Sub ' ERROR ''strDigest = Left(strDigest, nRet) Console.WriteLine("HMAC-MD5 =" & strDigest) Console.WriteLine("CORRECT =" & "750c783e6ab0b503eaa86e310a5db738") ' Compute HMAC-SHA-256 ''strDigest = String(API_MAX_HASH_CHARS, " ") strDigest = Mac.HexFromBytes(abData, abKey, MacAlgorithm.HmacSha256) If strDigest.Length <= 0 Then Exit Sub ' ERROR ''strDigest = Left(strDigest, nRet) Console.WriteLine("HMAC-SHA-256=" & strDigest) Console.WriteLine("CORRECT =" & "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843") ' Compute HMAC-SHA-512 ''strDigest = String(API_MAX_HASH_CHARS, " ") strDigest = Mac.HexFromBytes(abData, abKey, MacAlgorithm.HmacSha512) If strDigest.Length <= 0 Then Exit Sub ' ERROR ''strDigest = Left(strDigest, nRet) Console.WriteLine("HMAC-SHA-512=" & strDigest) Console.WriteLine("CORRECT =" _ & "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" _ & "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737")
[Contents]