Search code examples
c#jsonhmachmacsha1

Generating HMAC signature


I am trying to generate the HMAC signature. This is for a validation with credit card company. Below is my code to generate the HMAC signature:

public  string APISecurityKey(Identity postParameters)
        {
            String secretAccessKey = "secretkey";
            string  data = "message";
            byte[] secretKey = Encoding.UTF8.GetBytes(secretAccessKey);
            HMACSHA256 hmac = new HMACSHA256(secretKey);
            hmac.Initialize();
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            byte[] rawHmac = hmac.ComputeHash(bytes);
            return Convert.ToBase64String(rawHmac);

        }

They are asking me to substitute the message field with the JSON request (JSON supplied should have all carriage return newline sequences replaced with a space character). My object looks like so:

 public class Identity
    {
        public List<Header> header { get; set; }
        public string firstName { get; set; }
        public string LastName { get; set; }
        public string middleName { get; set; }
        public string address { get; set; }
        public string mothersName { get; set; }
        public string DriversLic { get; set; }
        public DateTime insertTime { get; set; }
        public string Status { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
        public DateTime UpdatedOn { get; set; }
    }
}


 public class Header
    {
        public string tenantId { get; set; }
        public string requestType { get; set; }
        public string clientReferenceId { get; set; }
        public string expRequestId { get; set; }
        public string txnId { get; set; }
        public string messageTime { get; set; }
        public string options { get; set; }


    } 

The example given to me for the message is like this:

{"tenantId": "TENANT1","request Type": "Simulator","clientRefere
nceId": "Hunter + ProveID","expRequestId": "333","messageTim
e": "2016-08-04T22:13:07Z","options": {"workflow": "wf1","model
Type":"mt1","responseType":1,"Responses": {…}}}

I am not sure how to include tenantId, tenantId, expRequestId, options as part of my JSON message. I am very new to JSON. I have this partial code:

public static async Task createJSON( Identity postParameters)
        {
            var tmpRequest = new HttpRequestMessage();
            tmpRequest.Content = new StringContent(JsonConvert.SerializeObject(postParameters));

            tmpRequest.Content.Headers.Add("tenantId", "V123456");
            tmpRequest.Content.Headers.Add("requestType", "PreciseIdOnly");
        }

postParameters is my Identity object with values. How can I create that JSON that includes tenantId, tenantId, expRequestId and the object so that I can put that JSON value in the message of APISecurityKey method. Below is the test JSON file that I need to create:

{
    "header": {
        "tenantId": "V123456",
        "requestType": "Precise",
        "clientReferenceId": "11111111",
        "RequestId": "",
        "txnId": "",
        "messageTime": "2020-05-28T00:00:02Z",
        "options": {}
    },
    "payload": {
        "control": [
            {
                "option": "SUBSCRIBER_PREAMBLE",
                "value": "wer"
            },
            {
                "option": "SUBSCRIBER_OPERATOR_INITIAL",
                "value": "WS"
            },
            {
                "option": "SUBSCRIBER_SUB_CODE",
                "value": "1111111"
            },
            {
                "option": "PID_USERNAME",
                "value": "demo"
            },
            {
                "option": "PID_PASSWORD",
                "value": "password"
            },
            {
                "option": "PRODUCT_OPTION",
                "value": "11"
            }
        ],
        "contacts": [{
            "id": "APPLICANT_CONTACT_ID_1",
            "person": {
                "typeOfPerson": "",
                "personIdentifier": "",
                "personDetails": {
                    "dateOfBirth": "1990-12-11",
                    "yearOfBirth": "",
                    "age": "",
                    "gender": "",
                    "noOfDependents": "",
                    "occupancyStatus": "",
                    "mothersMaidenName": "",
                    "spouseName": ""
                },
                "names": [{
                    "id": "",
                    "firstName": "Test1",
                    "middleNames": "D",
                    "surName": "Test2",
                    "nameSuffix": ""
                }]
            },
            "addresses": [{
                "id": "Main_Contact_Address_0",
                "addressType": "CURRENT",
                "poBoxNumber": "",
                "street": "2312 Test Drve",
                "street2": "",
                "postTown": "test",
                "postal": "49548",
                "stateProvinceCode": "CA"
            }],
            "identityDocuments": [{
                "documentNumber": "123456789",
                "hashedDocumentNumber": "",
                "documentType": "SSN"
            }]

        }]
    }
}

I want to generate the same JSOn as the sample above in C# using the objects.

any help will be highly appreciated.


Solution

  • If you using Visual studio, you can create the JSON by going to Edit menu and paster JSON as class. This will create all the classes for you and then you can assign values to the classes by instantiating them