Вопрос

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 from php 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.

Это было полезно?

Решение

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

Другие советы

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.

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 :)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top