未加星标

(Not-so)Clever key is causing problems with SHA512 Hmac in Node JS

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

This is an oddball question, but I have been working on this for hours now and am not making much progress. I am hoping someone here may be able to advise...

I am porting a script fromphp to node. The php script makes use of this function:

hash_hmac('sha512', $text, $key);

I have reproduced this in node using the crypto module:

var hash = crypto.createHmac( "sha512", key ); hash.update( text ); return hash.digest( "hex" );

I have verified that these functions produce the same hash when given the same text and key.

Except...

The string that is being used for a key in php looks similar to this: (Don't ask)

define("SITE_KEY", " __ , ,' e`---o (( ( | ___,' \\~-------------------------------' \_;/ ( / /) ._______________________________. ) (( ( (( ( ``-' ``-' ");

I have tried to reproduce this in javascript like so:

var key = "\ __\ , ,' e`---o\ (( ( | ___,'\ \\\\~-------------------------------' \\_;/\ ( /\ /) ._______________________________. )\ (( ( (( ( \ ``-' ``-'\ \ ";

But it doesn't work. (I assume it has to have something to do with the linebreaks).

Replacing the newlines with "\r\n" or "\n" as in the following also does not work:

var key = "\r\n __\r\n , ,' e`---o\r\n (( ( | ___,'\r\n \\\\~-------------------------------' \\_;/\r\n ( /\r\n /) ._______________________________. )\r\n (( ( (( ( \r\n ``-' ``-'\r\n\r\n";

Suggestions on how to fix this? (Getting rid of the dog is not an option, unfortunately.)

Thanks (in advance) for your help.

Problem courtesy of: user1031947

Solution

Why not store the string BASE64 encoded? That way you don't need to worry about line breaks, whitespace, anything like that.

Seeing as your php code is storing the key (apparently) correctly, try a script like:

<? $doggy_key = ....; echo base64_encode($doggy_key);

Run it from the command-line, copy the encoded key, then use it in your javascript. Decoding base64 strings is a simple problem, for example; Base64 encoding and decoding in client-side Javascript

Solution courtesy of: MrTrick

Discussion

Try replacing all of the newlines in the PHP version with \n (and no newline) in the JS version. If that doesn't work, try replacing them all with \r\n ― I bet PHP doesn't translate multiline literals in windows-formatted source :)

Discussion courtesy of: hobbs

There are no line breaks in the original string, to my knowledge. The backslash at the end tells PHP that the next line is just a continuation of the previous. To test whether I'm right or not, you could get PHP to print out the string and see if it has line breaks in it. But I suspect that you can write this in one long, or joined, string in Javascript.

Discussion courtesy of: Paul Wayper

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:(Not-so)Clever key is causing problems with SHA512 Hmac in Node JS
本站链接:https://www.codesec.net/view/627756.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(223)