计算 HMAC-SHA 签名

发布于 2024-10-31 18:43:24 字数 341 浏览 5 评论 0原文

我正在为 Amazon 的 SimpleDB 编写一个模块。它们要求使用 HMAC-SHA 算法对 REST 请求进行签名。 (详细信息请参见此处。

我被告知有是计算此签名的函数,但我在 文档。该函数被称为什么?它的参数是什么样的?

I'm writing a module for Amazon's SimpleDB. They require REST requests to be signed using HMAC-SHA algorithm. (Details here.)

I'm told that there is a function to computer this signature, but I can't find it in the documentation. What is the function called, and what do its arguments look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

权谋诡计 2024-11-07 18:43:24

编辑:以下内容应该有效:

pre {
  message = "Four score and seven years ago";
  key = "Abe Lincoln";
  signature = math:hmac_sha256_base64(message, key);
}
notify("Signature is", signature);

函数为 math:hmac_sha256_base64(,)

EDITED: The following should work:

pre {
  message = "Four score and seven years ago";
  key = "Abe Lincoln";
  signature = math:hmac_sha256_base64(message, key);
}
notify("Signature is", signature);

The function is math:hmac_sha256_base64(<datastring>,<keystring>)

冬天旳寂寞 2024-11-07 18:43:24

HMAC 是一个标准函数,可以在大多数平台的加密库中找到。亚马逊在您链接到的文档页面上显示了几个示例。

对于 Java,您可以在 javax.crypto 中找到它.Mac

对于 .Net,请查看系统.Security.Cryptography

对于KRL,我还没有找到任何内置的库存解决方案。由于嵌入 javascript 似乎是可能的,因此 jsSHA 实现可能会很有用。数学库中有一个sha1函数,并根据RFC2104似乎并不那么困难。

The HMAC is a standard function that can be found in crypto libraries for most platforms. Amazon shows several examples on the documentaion page you link to.

For Java, you can find it in javax.crypto.Mac

For .Net, look in System.Security.Cryptography

For KRL, I haven't found any built-in stock solution. Since it seems possible to embed javascript, the jsSHA implemenation could be useful. There is a sha1 function in the math library, and implementing HMAC according to RFC2104 doesn't seem that difficult.

不交电费瞎发啥光 2024-11-07 18:43:24

要使用嵌入的换行符对字符串进行签名(AWS 我在跟你说话!),您必须执行以下操作(基于 AWS S3 示例)

pre {
  raw_string = uri:unescape("GET%0A%0A%0AWed, 28 Mar 2007 01:29:59 +0000%0A/");
  sample_key = "uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o";
  signature = math:hmac_sha1_base64(raw_string, sample_key);
  expected = "Db+gepJSUbZKwpx1FR0DLtEYoZA=";
  passfail = (signature eq expected) => "pass" | "fail";
}

uri:decode() 函数返回带有正确换行符的字符串,其中 \n\n\n 没有。您可能需要在签名中添加尾随“=”。

To sign strings with embedded newlines (AWS I'm talking to you!) you have to do the following (based on the AWS S3 example)

pre {
  raw_string = uri:unescape("GET%0A%0A%0AWed, 28 Mar 2007 01:29:59 +0000%0A/");
  sample_key = "uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o";
  signature = math:hmac_sha1_base64(raw_string, sample_key);
  expected = "Db+gepJSUbZKwpx1FR0DLtEYoZA=";
  passfail = (signature eq expected) => "pass" | "fail";
}

The uri:decode() function returns a string with proper newlines, where \n\n\n does not. You might have to add trailing '=' to the signature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文