Help please. I can't get the Hash values to match. I'm trying to follow the instructions from the API Upgrade guide here: https://developer.authorize.net/support/hash_upgrade/
Step 1 - I've generated my signature key and stored it in my web.config
Step 2 - Convert the Signature Key into a byte array. I've skipped this step because, "For C# users, Authorize.Net provides the following code for converting the Signature Key into a byte array and calculating the HMAC-SHA512 hash."
Step 3 - create a message string from the API Login ID, the transaction ID and the amount.
Dim transactionRequest = New transactionRequestType With {
.transactionType = AuthOrAuthCapture,
.amount = amount,
.payment = paymentType,
.billTo = billingAddress,
.order = OrderInfo
}
So my message string looks like this:
Dim MessageString As String = String.Format("^{0}^{1}^{2}^", ApiLoginID, APITransID, amount)
Step 4 - "Use HMAC-SHA512 to hash the byte array form of the Signature Key from Step 2 with the message string from Step 3" - But I'm combining step 2 & step 4 using the provided c# code (converted to vb):
Public Function HMACSHA512(ByVal key As String, ByVal textToHash As String) As String
If String.IsNullOrEmpty(key) Then Throw New ArgumentNullException("HMACSHA512: key", "Parameter cannot be empty.")
If String.IsNullOrEmpty(textToHash) Then Throw New ArgumentNullException("HMACSHA512: textToHash", "Parameter cannot be empty.")
If key.Length Mod 2 <> 0 OrElse key.Trim().Length < 2 Then
Throw New ArgumentNullException("HMACSHA512: key", "Parameter cannot be odd or less than 2 characters.")
End If
Try
Dim k As Byte() = Enumerable.Range(0, key.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(key.Substring(x, 2), 16)).ToArray()
Dim hmac As HMACSHA512 = New HMACSHA512(k)
Dim HashedValue As Byte() = hmac.ComputeHash((New System.Text.ASCIIEncoding()).GetBytes(textToHash))
Return BitConverter.ToString(HashedValue).Replace("-", String.Empty)
Catch ex As Exception
Throw New Exception("HMACSHA512: " & ex.Message)
End Try
End Function
Step 5 - Compare the value of transHashSHA2 with the output from the HMAC-SHA512 hash mentioned in Step 4:
Dim myTransHashSha2 As String = response.transactionResponse.transHashSha2
myTransHashSha2 should equal hashedSignatureAndMessage , but it doesn't.
Where is the issue in my code? Thanks in advance!
Solved! Go to Solution.
03-13-2019 10:56 AM
So there must have been something in that translation. I created a separate project in c# that held just the function copied directly from the upgrade guide. Then I published and included the .dll in my vb project. So I'm calling the function like this:
Dim hashedSignatureAndMessage As String = ANHMACSHA512.HMACSHA512(SignatureKey, MessageString)
Now the hashes are matching!
03-20-2019 06:38 AM
03-17-2019 11:13 AM
Thanks for your reply. Yes in c# you need == but vb uses = for both comparing and assigning. However, maybe I need to take another look at my translation from the posted c# to my vb.
03-18-2019 07:07 AM
So there must have been something in that translation. I created a separate project in c# that held just the function copied directly from the upgrade guide. Then I published and included the .dll in my vb project. So I'm calling the function like this:
Dim hashedSignatureAndMessage As String = ANHMACSHA512.HMACSHA512(SignatureKey, MessageString)
Now the hashes are matching!
03-20-2019 06:38 AM