Add lua-module for signing
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
local sign = {}
|
||||
|
||||
local sha256 = require("sha256")
|
||||
|
||||
function sign.sign(key, secret, time, host, path, headers, contentSha256, region)
|
||||
local day = os.date("%Y%m%d", time)
|
||||
local date = os.date("%a, %d %b %Y %H:%M:%S GMT", time)
|
||||
local timestamp = os.date("%Y%m%dT%H%M%SZ", time)
|
||||
local dateKey = sha256.hmac_sha256("AWS4" .. secret, day)
|
||||
local dateRegionKey = sha256.hmac_sha256(dateKey, region)
|
||||
local dateRegionSvcKey = sha256.hmac_sha256(dateRegionKey, 's3')
|
||||
local signingKey = sha256.hmac_sha256(dateRegionSvcKey, 'aws4_request')
|
||||
print("DateKey: " .. dateKey)
|
||||
print("DateRegionKey: " .. dateRegionKey)
|
||||
print("DateRegionSvcKey: " .. dateRegionSvcKey)
|
||||
print("SigningKey: " .. signingKey)
|
||||
|
||||
headers["x-amz-content-sha256"] = contentSha256
|
||||
headers["date"] = date
|
||||
headers["host"] = host
|
||||
|
||||
local keys = {}
|
||||
for k in pairs(headers) do
|
||||
table.insert(keys, k)
|
||||
end
|
||||
table.sort(keys)
|
||||
|
||||
local signedHeaders = ""
|
||||
|
||||
local request = "PUT\n" .. path .. "\n"
|
||||
for _, k in ipairs(keys) do
|
||||
request = request .. "\n" .. k .. ":" .. headers[k]
|
||||
signedHeaders = signedHeaders .. ";" .. k
|
||||
end
|
||||
signedHeaders = string.sub(signedHeaders, 2)
|
||||
|
||||
request = request .. "\n\n" .. signedHeaders .. "\n" .. contentSha256
|
||||
print("Request:\n" .. request)
|
||||
print("-------")
|
||||
local stringToSign = "AWS4-HMAC-SHA256\n" .. timestamp .. "\n" .. day .. "/" .. region .. "/s3/aws4_request\n" .. sha256.sha256(request)
|
||||
print("String to sign:\n" .. stringToSign)
|
||||
print("-------")
|
||||
local signature = sha256.hmac_sha256(signingKey, stringToSign)
|
||||
print("Signature:\n" .. signature)
|
||||
print("-------")
|
||||
|
||||
local result = "AWS4-HMAC-SHA256 Credential=" .. key .. "/" .. day .. "/" .. region .. "/s3/aws4_request,SignedHeaders=" .. signedHeaders
|
||||
result = result .. ",Signature=" .. signature
|
||||
return result
|
||||
end
|
||||
|
||||
return sign
|
||||
Reference in New Issue
Block a user