0

I have an issue while I am creating the HMAC in LUA 5.1 version and same code is working in node.js

Node.js Code :

crypto.createHmac(
  CONSTANTS.HMAC_ALORITHM_SHA,
  Buffer.from(
    secretAccessKey,
    CONSTANTS.BASE64_ENCODING
  )
).update(
  Buffer.from(
    stringToSign, 
    CONSTANTS.UTF8
  )
).digest(
  CONSTANTS.BASE64_ENCODING
);

I wanted to write in the same code LUa 5.1.

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0
local sha2 = require("sha2")
local your_hmac_as_hex_string = sha2.hmac(
   sha2.sha1,  -- SHA-1
   "secretAccessKey as binary string", 
   "stringToSign"
)
local your_hmac_as_binary_string = sha2.hex2bin(your_hmac_as_hex_string)

The library is here


UPDATE:

If your key is stored as base64, you need to convert it from base64 to binary:

local binary_key = sha2.base642bin(base64_key)
-- invoke sha2.hmac() here

And if you want to convert the result to base64:

local your_hmac_as_base64 = sha2.bin2base64(your_hmac_as_binary_string)
Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23
  • Thanks a lot for your response and I can hope it will work with sha256 . Will try and update . – Ashish Mishra Mar 16 '19 at 05:12
  • Thanks a ton for your response . unfortunately I am not getting same hash string what I am getting from node.js code .i have tried every-thigh below is node js code .. 1. let accesskey = Buffer.from('AWS KEY', 'base64') o/p - 2. let hmac = crypto.createHmac("sha256",accesskey) 3.let signString = Buffer.from(signString "utf-8"); let hmacUpdate = hmac.update(signString); hmacUpdateBase64 = hmacUpdate.digest("base64"); //final Hmac local LUA code your_hmac_as_hex_string = sha2.hmac( sha2.sha256, -- SHA-1 secretKeyBinary, signString ) – Ashish Mishra Mar 16 '19 at 17:14
  • @AshishMishra - Answer updated, module `sha2.lua` is also updated on GitHub. – Egor Skriptunoff Mar 17 '19 at 11:40
  • If it still doesn't work please show exact input arguments of your Lua script (and exact result string you're expecting) – Egor Skriptunoff Mar 17 '19 at 11:44
  • Sure @EgorSkriptunoff . I will try and update you .Thanks once again – Ashish Mishra Mar 17 '19 at 19:15
  • Thanks @EgorSkriptunoff .. its helps me a lot . now working – Ashish Mishra Mar 23 '19 at 04:43