使用Hmac和base64进行签名

问题描述 投票:0回答:1

我正在尝试连接到服务器,密码有点棘手。 密码应该这样生成

base64(hmac(secret,timestamp:userid)

我有秘密和用户 ID,我们可以将它们视为秘密和 0。 我正在使用 Flutter,在 Flutter hmac 函数中需要一个 HASH 和一个列表,所以我无法对两个字符串进行哈希处理/

你能帮忙解决这个问题吗?

flutter dart cryptography base64 hmac
1个回答
0
投票

crypto
包包含HMAC的实现。正如您所期望的,它需要一个密钥和一条消息。请参阅示例代码:

var key = utf8.encode('p@ssw0rd'); // key gets converted to bytes (using utf8)
var bytes = utf8.encode("foobar"); // message gets converted to bytes (utf8)

var hmacSha256 = Hmac(sha256, key); // HMAC-SHA256 (using the key bytes)
var digest = hmacSha256.convert(bytes); // the hmac is generated from the message

在你的情况下,你会使用:

var key = utf8.encode('SECRET'); 
var bytes = utf8.encode('0');

var hmacSha256 = Hmac(sha256, key); // HMAC-SHA256
var digest = hmacSha256.convert(bytes); 

所有 HMAC 都需要哈希函数。您需要找出您的 API 需要哪个 HMAC 哈希值。

© www.soinside.com 2019 - 2024. All rights reserved.