如何从头开始在VBA中实现hmac sha256?

时间:2017-10-26 01:07:29

标签: excel-vba cryptography vba excel

由于某些原因,我必须在Scratch的VBA中实现hmac sha256。

我已经编写了以下代码,但我没有得到正确的结果(为了验证,我使用下表:https://tools.ietf.org/html/rfc4231#section-4.2

以下是实施的代码:

在我的代码中,对应于SHA256的函数Hash是从外部库中使用的,并且工作正常(我可以检查结果)。我认为它的工作正常,但我很确定问题来自转换:十进制,十六进制,字节......这不是我掌握的好东西。

我从https://en.wikipedia.org/wiki/Hash-based_message_authentication_code

获得了公式

任何人都可以使用以下代码帮助您。这个HMAC实施有什么问题?

提前谢谢。

    Option Explicit

    Public Sub Main()
        'Key declaration
        Dim Key As String
        Dim KeyBytes() As Byte

        'Message declaration
        Dim Message As String
        Dim MessageBytes() As Byte

        'ipad, opad
        Dim iPad_() As Byte
        Dim oPad_() As Byte

        'Size
        Dim n As Long

        'Output
        Dim Result() As Byte
        Dim A() As Byte, B() As Byte

        n = 64

        'Define the key
        Key = "123"
        KeyBytes = Key  'Convert string to byte
        KeyBytes = CompleteKey(KeyBytes, n) 'At this step there are 64 Bytes in the key

        'Define the message
        Message = "TestMessage"
        MessageBytes = Message  'Convert string to byte

        'Define ipad, opad
        iPad_ = ipad(n)
        oPad_ = opad(n)

        'Get the XOR operation of the key and ipad
        B = XOR_(KeyBytes, iPad_)

        'Concatenate the result with the message
        B = Concatenate(B, MessageBytes)

        'Compute the final hash
        A = XOR_(KeyBytes, oPad_)
        Result = Hash(Concatenate(A, Hash(B)))
    End Sub

    Public Function CompleteKey(KeyBytes() As Byte, n As Long) As Byte()
        Dim O() As Byte

        If UBound(KeyBytes) < n Then
            O = KeyBytes
            ReDim Preserve O(n - 1)
        End If

        CompleteKey = O
    End Function

    Public Function XOR_(Item1() As Byte, Item2() As Byte) As Byte()
        Dim i As Long, j As Long
        Dim A As String, B As String
        Dim Output()  As Byte

        ReDim Output(UBound(Item2) - 1)

        For i = 0 To UBound(Item1) - 1
            A = WorksheetFunction.Dec2Bin(Item1(i), 8)
            B = WorksheetFunction.Dec2Bin(Item2(i), 8)
            Output(i) = WorksheetFunction.Bin2Dec(XorBinary(A, B))
        Next i

        For j = i To UBound(Item2) - 1
            Output(i) = Item2(j)
        Next j

        XOR_ = Output
    End Function

    Public Function XorBinary(x As String, y As String) As String
        Dim i As Long
        Dim n As Long
        Dim A As Long
        Dim B As Long
        Dim C As String
        Dim O As String

        n = Len(x)

        For i = 1 To n
            A = CLng(Right(Left(x, i), 1))
            B = CLng(Right(Left(y, i), 1))

            If A * B = 0 And A + B = 1 Then
                C = "1"
            Else
                C = "0"
            End If

            O = O & C
        Next i
    End Function


    Public Function ipad(n As Long) As Byte()
        Dim S() As Byte
        Dim i As Long

        ReDim S(n)

        For i = 1 To n
            S(i) = 36
        Next i

        ipad = S
    End Function

    Public Function opad(n As Long) As Byte()
        Dim S() As Byte
        Dim i As Long

        ReDim S(n)

        For i = 1 To n
            S(i) = 92   '5C
        Next i

        opad = S
    End Function

    Public Function Concatenate(A() As Byte, B() As Byte) As Byte()
        Dim C() As Byte
        Dim i As Long, j As Long

        ReDim C(UBound(A) + UBound(B) + 1)

        For i = 0 To UBound(A)
            C(i) = A(i)
        Next i

        For j = i To i + UBound(B) - 1
            C(j) = B(j - UBound(A) - 1)
        Next j

        Concatenate = C
    End Function

0 个答案:

没有答案