cancel
Showing results for 
Search instead for 
Did you mean: 

Transaction Hash Upgrade Guide for API using VB.NET. Hashes don't match

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.

  • I'm pulling my APILoginID from where I stored it in my web.config
  • I'm pulling the transaction ID this way:  Dim APITransID As String = response.transactionResponse.transId
  • The guide says to use "The transaction amount that we send in createTransactionResponse in the amount element. " However, there is no amount element in response.transactionResponse, so I'm using the same amount variable I'm using to send in the transaction:

 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):

  • SignatureKey is being pulled from my web.config
  • I'm calling the funciton with this line:
    Dim hashedSignatureAndMessage As String = HMACSHA512(SignatureKey, MessageString)

 

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!

 

JenHaga
Member
1 ACCEPTED SOLUTION

Accepted Solutions

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!

View solution in original post

3 REPLIES 3
@JenHaga

with a full disclosure that I’ve never written an app in C# when you are calculating the remainder (I think that is what you are doing with x Mod 2 = 0) you appear to be assigning a value when you need to be comparing a value. So change to Mod 2 == 0 and see what happens.
Renaissance
All Star

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. 

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!