Change SHA-implementation and make sure signature is correctly generated

This commit is contained in:
2019-08-02 10:27:54 +02:00
parent cd38b8b25e
commit fc9269f362
10 changed files with 436 additions and 807 deletions
+19 -21
View File
@@ -1,35 +1,33 @@
lu = require('luaunit')
local sign = require("sign")
local sha256 = require("sha256")
local sha2 = require("sha2")
--local key = os.getenv("AWS_ACCESS_KEY_ID")
--local secret = os.getenv("AWS_SECRET_ACCESS_KEY")
-- local headers = {["x-amz-acl"] = acl, ["x-amz-date"] = date, ["x-amz-content-sha256"] = "UNSIGNED-PAYLOAD"}
function TestPut()
function TestPutAWSExample()
local key = "AKIAIOSFODNN7EXAMPLE"
local secret = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
local time = os.time({year = 2013, month = 5, day = 24, hour = 0})
local headers = {["x-amz-storage-class"] = "REDUCED_REDUNDANCY", ["x-amz-date"] = "20130524T000000Z" }
local contentSha256 = sha256.sha256("Welcome to Amazon S3.")
local signature = sign.sign(key, secret, time, "examplebucket.s3.amazonaws.com", "/test%24file.text", headers, contentSha256, "us-east-1")
print(signature)
local date = os.date('%a, %d %b %Y %H:%M:%S GMT', time)
local path = "/test%24file.text"
local region = "us-east-1"
local contentSha256 = sha2.hash256("Welcome to Amazon S3.")
local headers = {["x-amz-storage-class"] = "REDUCED_REDUNDANCY", ["x-amz-date"] = "20130524T000000Z", ["x-amz-content-sha256"] = contentSha256, ["host"] = "examplebucket.s3.amazonaws.com", ["date"] = date }
local signature = sign.sign(key, secret, time,path, headers, region)
lu.assertEquals( type(signature), 'string' )
lu.assertEquals( signature, "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request,SignedHeaders=date;host;x-amz-content-sha256;x-amz-date;x-amz-storage-class,Signature=98ad721746da40c64f1a55b78f14c238d841ea1380cd77a1b5971af0ece108bd" )
end
function TestPut2()
local key = "AKIAI2BOGUMMDPX2WKSQ"
local secret = "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"
local time = os.time({year = 2019, month = 6, day = 28, hour = 15, min = 56, sec = 16})
local headers = {["x-amz-acl"] = "public-read", ["x-amz-date"] = "20190628T135616Z" }
local contentSha256 = sha256.sha256("fluff2")
local signature = sign.sign(key, secret, time, "upload.unbound.se.s3-eu-west-1.amazonaws.com", "/20190628/d25f1855e0cab108cb898ca2bebbb35ed228c1134f9d89492f2c705d5b3c6c355767d37a823ccb31", headers, contentSha256, "eu-west-1")
print(signature)
function TestPut()
local key = "AKIAIOSFODNN7EXAMPLE"
local secret = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
local time = os.time({year = 2019, month = 6, day = 28, hour = 23, min = 50, sec = 58})
local date = os.date('%a, %d %b %Y %H:%M:%S GMT', time)
local path = "/20190628/d98e63986091dc0eb0fa24cff9fffaf17b90c464c2b1f05685bc91e7585267cb0d09c645e69ef2b7"
local region = "eu-west-1"
local contentSha256 = sha2.hash256("fluff2")
local headers = {["x-amz-acl"] = "public-read", ["x-amz-date"] = "20190628T235058Z", ["x-amz-content-sha256"] = contentSha256, ["host"] = "upload.unbound.se.s3-eu-west-1.amazonaws.com", ["date"] = date }
local signature = sign.sign(key, secret, time,path, headers, region)
lu.assertEquals( type(signature), 'string' )
lu.assertEquals( signature, "AWS4-HMAC-SHA256 Credential=AKIAI2BOGUMMDPX2WKSQ/20190628/eu-west-1/s3/aws4_request,SignedHeaders=date;host;x-amz-acl;x-amz-content-sha256;x-amz-date,Signature=38683969ed1a023aedbd0e14bf4beae48a6430c6e13c93f59257c5596578721d" )
lu.assertEquals( signature, "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20190628/eu-west-1/s3/aws4_request,SignedHeaders=date;host;x-amz-acl;x-amz-content-sha256;x-amz-date,Signature=6dd94b1ff7fd29251dbec629b813ea974323aa6509b6a3db94744aef386914af" )
end
-- class TestSign