I\'m trying to write an HMAC function in Classic ASP using SHA256 as the hash. I thought I got it right, but the results aren\'t the same for the examples listed on the Wiki
The issue is because VBScript String is not a bytearray, so when you concat chr(0) you are adding 2 0 bytes to string.
Anyway VBScript is a nighmare for managing bytes and words, so I suggest you use a crypt javascript API that we have wrapped for ASP VBScript use.
You could get more details, and our contact info in this repository: https://github.com/ictmanagement/redsysHMAC256_API_ASP
A complete example:
'/****** MAC Function ******/
'Input String|WordArray , Returns WordArray
Function mac256(ent, key)
Dim encWA
Set encWA = ConvertUtf8StrToWordArray(ent)
Dim keyWA
Set keyWA = ConvertUtf8StrToWordArray(key)
Dim resWA
Set resWA = CryptoJS.HmacSHA256(encWA, keyWA)
Set mac256 = resWA
End Function
'Input (Utf8)String|WordArray Returns WordArray
Function ConvertUtf8StrToWordArray(data)
If (typename(data) = "String") Then
Set ConvertUtf8StrToWordArray = CryptoJS.enc.Utf8.parse(data)
Elseif (typename(data) = "JScriptTypeInfo") Then
On error resume next
'Set ConvertUtf8StrToWordArray = CryptoJS.enc.Utf8.parse(data.toString(CryptoJS.enc.Utf8))
Set ConvertUtf8StrToWordArray = CryptoJS.lib.WordArray.create().concat(data) 'Just assert that data is WordArray
If Err.number>0 Then
Set ConvertUtf8StrToWordArray = Nothing
End if
On error goto 0
Else
Set ConvertUtf8StrToWordArray = Nothing
End if
End Function
Dim test
test = "Hi guys"
key = "guyb u oisd qiu dqid qew" 'You could create a WordArray from Hex String, Utf8 String, etc.
Dim res
res = mac256(test,key) 'Result is a WordArray, so
Response.Write res.toString(CryptoJS.enc.Hex)