简体   繁体   English

在javascript中的HMAC SHA256十六进制摘要

[英]HMAC SHA256 hex digest in javascript

I'm trying to write a javascript lib to push events to Pusher. 我正在尝试编写一个javascript库来将事件推送给Pusher。

In the environment I'm using I can't use nodejs, so no require('crypto') ... or at least I do not know of a way of using it outside node. 在我正在使用的环境中我不能使用nodejs,所以没有require('crypto')......或者至少我不知道在节点外使用它的方法。

I'm following the example guide here: http://pusher.com/docs/rest_api ; 我在这里关注示例指南: http//pusher.com/docs/rest_api ; I'm stuck in the part where it says I should generate the HMAC SHA256 hex digest. 我陷入了它应该生成HMAC SHA256十六进制摘要的部分。

I am using this library http://code.google.com/p/crypto-js/#HMAC 我正在使用此库http://code.google.com/p/crypto-js/#HMAC

So, following the instructions on Pusher i wrote 所以,按照我写的Pusher的说明

CryptoJS.HmacSHA256(
  'POST\n/apps/3/channels/project-3/events\nauth_key=278d425bdf160c739803&auth_timestamp=1272044395&auth_version=1.0&body_md5=7b3d404f5cde4a0b9b8fb4789a0098cb&name=foo',
  '7ad3773142a6692b25b8'
);

But that's giving me 20b132baa2aaf44ea1fab814f0788aaa44eed23a2d252c72e4dc2aaea0d6ac24 instead of b3152b2bb5a5b0bae66435a498797aa763e9b74f86cc97c9175424f8308b2f80 . 但那给了我20b132baa2aaf44ea1fab814f0788aaa44eed23a2d252c72e4dc2aaea0d6ac24而不是b3152b2bb5a5b0bae66435a498797aa763e9b74f86cc97c9175424f8308b2f80

What is bothering me is that I didn't do the "hex digest" part, so maybe that's what I'm missing, but I couldn't find a suitable lib to do this in javascript. 困扰我的是我没有做“十六进制摘要”部分,所以也许这就是我所缺少的,但我找不到合适的lib来在javascript中执行此操作。 Do you spot any errors in what I did? 你发现我做的任何错误吗? Do you have a library to suggest? 你有图书馆建议吗?

Thanks in advance for any help. 在此先感谢您的帮助。

I was struggling with doing the exact same thing. 我正在努力做同样的事情。 Pusher's documentation should really be clearer about this issue. Pusher的文档应该更清楚这个问题。 It turns out it's not the encyption methods. 事实证明,这不是加密方法。 It's the order that you list your parameters. 这是您列出参数的顺序。 The body of your message has to be exactly formatted like this: 您的邮件正文必须完全按照以下格式进行格式化:

{"data":"{\\"message\\":\\"hello world\\"}","name":"my_event","channel":"test_channel"} {“data”:“{\\”message \\“:\\”hello world \\“}”,“name”:“my_event”,“channel”:“test_channel”}

Data has to be first, then message, followed by name and then finally the channel name. 数据必须是第一个,然后是消息,然后是名称,最后是通道名称。

Then when you create your auth signature you have to list your parameters as so: 然后,当您创建身份验证签名时,您必须列出您的参数:

POST\\n/apps/3/events\\nauth_key=278d425bdf160c739803&auth_timestamp=1353088179&auth_version=1.0&body_md5=ec365a775a4cd0599faeb73354201b6f POST \\ N /应用/ 3 /事件\\ nauth_key = 278d425bdf160c739803&auth_timestamp = 1353088179&auth_version = 1.0&body_md5 = ec365a775a4cd0599faeb73354201b6f

So auth key is first, then the timestamp, followed by the auth version and finally the body md5. 首先是auth键,然后是时间戳,然后是auth版本,最后是body md5。

I got it to work once I followed this exact pattern. 一旦我遵循这个确切的模式,我得到了它的工作。 Hopefully this helps! 希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM