Postgresql hmac-sha1 签名与 Python 签名不同

2 投票
1 回答
517 浏览
提问于 2025-04-18 17:38

Postgres:

代码:

SELECT encode(
    hmac(
        E'PUT\n\n1\n1408355972\nx-amz-acl:bucket-owner-full-control\n/1/1', 
        '1sf235123',
        'sha1'
    ),
    'base64'
);

结果:"h9wRL15mXgwRxXjqLqhbYbnfJ7I="

Python 3

代码:

base64.encodestring(
    hmac.new(
        'PUT\n\n1\n1408355972\nx-amz-acl:bucket-owner-full-control\n/1/1'.encode(),
        '1sf235123'.encode(),
        sha1
    ).digest()
)

结果:"CrU1V93ggf3QE0ovq686ir/i1ss=\n"

我想在Postgres中生成一个S3上传请求的签名,但我一直得不到正确的签名,今天试了整整一天T_T。

有人能帮帮我吗??非常感谢。

1 个回答

2

你在Python中交换了前两个hmac参数的位置。其实hmac的构造函数应该先放秘密密钥。

>>> base64.encodestring(
...     hmac.new(
...         '1sf235123'.encode(),
...         'PUT\n\n1\n1408355972\nx-amz-acl:bucket-owner-full-control\n/1/1'.encode(),
...         sha1
...     ).digest()
... )
b'h9wRL15mXgwRxXjqLqhbYbnfJ7I=\n'

撰写回答