OpenSSL - 作为 Lua encdec.hmacsha256() 的替代方法

我正在尝试将使用我找不到的模块的 Lua 脚本进行重新平台化。我卡在了 encdec.hmacsha256() 上,该命令用于此特定命令 sig = encdec.hmacsha256(ciphertext, hmac_key, true)

我想使用 openSSL 来创建签名,但我无法成功构建所需的命令行。请有人帮帮我吗?

(FYI-我是一个新手,当涉及到加密和密码等方面时。)

下面是 Lua 代码,其中它适合...

local hmac_key = createHMACKey()
print ("Returned hmac_key = " ..hmac_key)
local hmac_key_HEX = binascii.hexlify(hmac_key)
print ("Returned hmac_key_HEX = " ..hmac_key_HEX)
--------------------------------

--orig = local sig = encdec.hmacsha256(ciphertext, hmac_key, true)
local opensslHMAC256command = "openssl sha256 -hex -mac HMAC -macopt hexkey:" ..ciphertext..hmac_key
local command = assert(io.popen(opensslHMAC256command, 'r'))
local output = command:read('*all')
command:close()

print("opensslHMAC256command in = " ..output)
local file = "etc/encryptedpayload1.txt"
local outf = io.open(file, "w")
outf:write(payload)
outf:close()

local file = "etc/encryptedpayload2.txt"
local outf = io.open(file, "r")
local encreading = outf:read("*all")
print("opensslHMAC256command out = " ..encreading)
outf:close()

local sig = encreading
print("sig = " ..sig)

原文链接 https://stackoverflow.com/questions/70961227

点赞
stackoverflow用户1847592
stackoverflow用户1847592

你可以用以下代码替换:

local sha = require("sha2")
local sig = sha.hex_to_bin(sha.hmac(sha.sha256, hmac_key, ciphertext))

这个库可以在这里找到。

2022-02-02 19:45:12
stackoverflow用户6676439
stackoverflow用户6676439

在我个人的情况下,我们不得不修改 require sha2 模块以及相关的 local 调用,(添加了一个.z =选项),以便正确请求该函数

local sha = require("sha2").z
local sig = sha.hex_to_bin(sha.hmac(sha.sha256, hmac_key, ciphertext))
2022-02-18 10:29:35