As you can see I get same requestSignatureBase64String if I use string values in js and objectscript examples. Unfortunately web service is expecting value QxYkJH+b0xo+pCLMBMXgKhMqjMSV+mqQbNvPf7kX2k4=
I get same signature and secretByteArray values in js and objectscript, but difference is that objectscript value is String and js value is 32-bit WordArray.
How do I get correct signature and secretByteArray values that I can pass to the $SYSTEM.Encryption.HMACSHA method?
Cryptojs example
var message = "messageinabottle"
var secretByteArray = CryptoJS.enc.Base64.parse(key) //b1e72b7ad91e
var requestSignatureBase64String = CryptoJS.enc.Base64.stringify(signatureBytes) //QxYkJH+b0xo+pCLMBMXgKhMqjMSV+mqQbNvPf7kX2k4=
var requestSignatureBase64String = CryptoJS.enc.Base64.stringify(signatureBytes) //OrUVho2ak9cwJXiVEPn8QInT+0hyL7kJ9zMcx+E/Zxk=
ObjectScript example
Set message = "messageinabottle"
Set secretByteArray = $ZCONVERT(##class(%xsd.hexBinary).LogicalToXSD(##class(%SYSTEM.Encryption).Base64Decode(key)),"L") //b1e72b7ad91e
Set requestSignatureBase64String = ##class(%SYSTEM.Encryption).Base64Encode(signatureBytes) //kQeBc7Gu4SRUeicA7xVeN6V9sWX5b1bCLX9rUBK+lGA=
Set requestSignatureBase64String = ##class(%SYSTEM.Encryption).Base64Encode(signatureBytes) //OrUVho2ak9cwJXiVEPn8QInT+0hyL7kJ9zMcx+E/Zxk=
Hi Jorge and thanks for the answer!
Yes there is external system where I try to authenticate and with Postman that javasript works just fine.
Set tSecretByteArray = ..hex(##class(%SYSTEM.Encryption).Base64Decode(keyUTF8))
So do I have to get 32-bit wordArray like in JS var signature
= CryptoJS.enc.Utf8.parse(rawData);
If do some hardcoding and set "signature
" value from Postman, I won't get right values from
Set tSignature = ..hex($SYSTEM.Encryption.HMACSHA(256, wordArray, tSecretByteArray))
Set tSignatureBase64 = ##class(%SYSTEM.Encryption).Base64Encode(tSignature)
And yes, to_hmac was copy-paste error ;)
Solution was easier than I thought.
Set key = "secretkey"
Set message = "messageinabottle"
Set secretByteArray = ##class(%SYSTEM.Encryption).Base64Decode(key)
Set signatureBytes = $SYSTEM.Encryption.HMACSHA(256, message, secretByteArray)
Set requestSignatureBase64String = ##class(%SYSTEM.Encryption).Base64Encode(signatureBytes)