Creating a valid sha1 signature for API

369 Views Asked by At

I am trying to post to an API that requires a SHA1 signature

https://www.inkthreadable.co.uk/api-documentation#order-create

The Signature value will need to be generated at your side as SHA1(request body + Secret Key).

This is the method I am using to create the signature

 static string ComputeSignature(byte[] body, byte[] secret)
    {
        using (SHA1Managed sha1 = new SHA1Managed())
        {
            var key1 = sha1.ComputeHash(body);
            var key2 = key1.Concat(secret).ToArray();
            var key3 = sha1.ComputeHash(key2);
            return Convert.ToBase64String(key3);
        }
    }




  var body = Encoding.UTF8.GetBytes(json);
        var secret = Encoding.UTF8.GetBytes(_inkThreadableSettings.Value.SecretKey);
        var signature1 = ComputeSignature(body, secret);

I'm using rest client (vs code extension) to post the data. The json section below is what i am using to pass to the ComputeSignature method above (body param)

POST https://www.inkthreadable.co.uk/api/orders.php?AppId=APP-00202123&Signature=cbAJNd4lU+/+OXpN2bsVJczOuo8=
content-type: application/json

{
   "brandName":"Inkthreadable",
   "comment":"Test order.",
   "shipping_address":{
      "firstName":"Alex",
      "lastName":"Cunliffe",
      "company":"Inkthreadable",
      "address1":"Unit 501a",
      "address2":"Glenfield Park Two",
      "city":"Blackburn",
      "county":"Lancashire",
      "postcode":"BB1 5QH",
      "country":"United Kingdom",
      "phone1":"+44 (0)1254 777070"
   },
   "shipping":{
      "shippingMethod":"courier"
   },
   "items":[
      {
         "pn":"JH001",
         "quantity":4,
         "retailPrice":20,
         "description":"Please print as large as posible",
         "label":{
            "type":"printed",
            "name":"ink-label"
         },
         "designs":{
            "front":"http://animalfair.com/wp-content/uploads/2014/06/little_cute_cat_1920x1080.jpg",
            "back":"http://data3.whicdn.com/images/168204223/large.jpg"
         }
      }
   ]
}

The response I get is : "Invalid Signature"

I have tried a number of permutations of a compute signature function but not got it working.

Also tried this:

private string ComputeSignature7(byte[] body, byte[] secret)
    {

        using (SHA1Managed sha1 = new SHA1Managed())
        {
            var key = sha1.ComputeHash(body.Concat(secret).ToArray());
           
            return Convert.ToBase64String(key);
        }
    }
0

There are 0 best solutions below