我在LUA 5.1版本中创建HMAC时遇到问题,同样的代码在node.js中工作
Node.js代码:
crypto.createHmac(
CONSTANTS.HMAC_ALORITHM_SHA,
Buffer.from(
secretAccessKey,
CONSTANTS.BASE64_ENCODING
)
).update(
Buffer.from(
stringToSign,
CONSTANTS.UTF8
)
).digest(
CONSTANTS.BASE64_ENCODING
);
我想用相同的代码编写LUa 5.1。
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)
图书馆是here
更新:
如果您的密钥存储为base64,则需要将其从base64转换为二进制:
local binary_key = sha2.base642bin(base64_key)
-- invoke sha2.hmac() here
如果要将结果转换为base64:
local your_hmac_as_base64 = sha2.bin2base64(your_hmac_as_binary_string)