I am integrating with an API. The author of the API provided me with a Postman pre-request script. That script uses crypto-js - something I have never used before.
In the example, they stringify the JSON payload, parse that as UTF8, in turn parse that as base64, encrypt it using HmacSHA256, parse that back to UTF8, and then stringify that to base 64
I am not sure how I can replicate what the JS is doing to the C# equivalent.
Here is JS code
var crypto = require("crypto-js");
pm.globals.set("SecretKey", "key");
var key = pm.globals.get("SecretKey");
var payload = {
"vendor": {
"email": "",
"firstName": "",
"lastName": ""
},
"purchaseId": "12345678"
};
var payloadString = JSON.stringify(payload);
var payloadUTF8 = crypto.enc.Utf8.parse(payloadString);
var payloadBase64 = crypto.enc.Base64.stringify(payloadUTF8);
pm.environment.set("Payload", payloadBase64);
var signature = crypto.HmacSHA256(payloadUTF8, key);
var signatureUTF8 = crypto.enc.Utf8.parse(signature);
var signatureBase64 = crypto.enc.Base64.stringify(signatureUTF8);
pm.environment.set("Signature", signatureBase64);
In the C# this is as far as I have got:
// payload is JSON object which matches the above.
var payloadString = JsonConvert.SerializeObject(payload);
byte[] bytes = Encoding.Default.GetBytes(payloadString);
var payloadUTF8 = Encoding.UTF8.GetString(bytes);
I am not sure of the corresponding class in the System.Security.Crypto namespace.
