Search code examples
phpcodeigniterapihashhmacsha1

Generate HMAC SHA1 for Rail PNR API in PHP


I am trying to generate the HMAC SHA1 Hash to use the Railway PNR API.

I am using PHP Framework Codeigniter. I used the helper given here.

Then I tried to generate the hash and compare it with the Hash Generator given in the Rail PNR API site.

But both the hash i.e. the one I generated and the one I am getting from the Rail PNR API site are not the same.

Then I tried to use online tools to generate the hash value for the same. But still the hash that is generated is not same as that is coming from the API site.

My three data are in the following way:

  • PNR: 1234567890
  • Format: json
  • Public Api Key: <My Public Api Key>

So the data that has to be hashed is: 1234567890json<My Public Api Key> using the <My Private Api Key>

I don't know that if the method to generate hash is wrong or if I am doing it wrong. I pretty much followed every step that is stated there correctly.

I am okay if the solution is available in javascript, but PHP is more preferred.

EDIT:

I even read the following but they couldn't help:

-- Trying to digitally sign via HMAC-SHA1 with PHP

-- OpenSSL HMAC-SHA1 digest does not match Crypto's

Please help me out here...

Thanks.


Solution

  • The values that are to be passed in the API is supposed to be in the alphabetical order. Like in my case above, if we arrange the keys in alphabetical order the keys will be: format, pbapisign (i.e. the public key) and pnr.

    So the string to be hashed will be 'json'.'<My Public API Key>'.'1234567890'.

    Now to generate the hash, basically you can use any PHP function. I use the hash_hmac() function to generate the hash.

    Now its working for me. Hope this helps anyone who's working with the Rail PNR API.

    Thanks.