1

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.

1
  • I guess we can assume you have the key in the form of a byte[] ? Commented May 22, 2018 at 22:31

1 Answer 1

2

This is pretty close... I am not sure about the second UTF8 encoding you do after the hash, because in C# the hash is already in a byte[].

        static void Main(string[] args)
        {
            var secretKey = "secret";

            var payload = new
            {
                vendor = new
                {
                    email = "",
                    firstName = "",
                    lastName = ""
                },
                purchaseId = "12345678"
            };

            string payLoadString = JsonConvert.SerializeObject(payload);
            //NOTE:  C# uses UTF-8 by default, so this is the same as Encoding.Default.GetBytes();
            byte[] payLoadUTF8 = Encoding.UTF8.GetBytes(payLoadString);  
            string payLoadBase64 = Convert.ToBase64String(payLoadUTF8);

            string signatureBase64 = null;
            using (HMACSHA256 hmac = new HMACSHA256(Encoding.Default.GetBytes(secretKey)))
            {
                byte[] hash = hmac.ComputeHash(payLoadUTF8);
                signatureBase64 = Convert.ToBase64String(hash);
            }

            Console.WriteLine($"payLoadBase64:\t{payLoadBase64}");
            Console.WriteLine($"signatureBase64: \t{signatureBase64}");
            Console.ReadLine();
        }

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.