Node: Crypto SHA1 - NodeJs v C#

Created on 16 Mar 2017  ·  3Comments  ·  Source: nodejs/node

  • Version: v7.7.2 / v6.10.0
  • Platform: Windows 10 - 64-bit
  • Subsystem: Buffer


Hi,

I'm using crypto module to create hmac using sha1 to hash, but when I'll hash a json (stringified) with words that contain accent, it returns to me a hash different from C#, look:

hash returned from C#
"9XbAUCCZI1hYbgEbVCjpFj0BKt0="

hash returned from NodeJs
"u2X+a93VC0La+q1o8JTS+ypA1sM="

Code used in C#:
```C#
var encoding = new ASCIIEncoding();
var json = "{\"city\":\"São Paulo\"}";

var bufferSecret = Convert.FromBase64String("c2VjcmV0IHBhc3M=");
var bufferJson = encoding.GetBytes(json);
var hmac = new HMACSHA1(bufferSecret);
var hashBytes = hmac.ComputeHash(bufferJson);
var hash = Convert.ToBase64String(hashBytes);


Code used in NodeJs:
```js
let json = JSON.stringify({"city":"São Paulo"});
let bufferSecret = new Buffer(`c2VjcmV0IHBhc3M=`, "base64");
let bufferJson = new Buffer(json, "ascii");
let hash = crypto.createHmac("sha1", bufferSecret).update(bufferJson).digest("base64");

Yes, I know that I must to use UTF8 encoding to Buffer (but the API apparently just accept ASCII encode to encrypt the 'message'), but look the conversion json to bytes, are not (as mentioned by @AdamMajer, thank you) the same:
image
image

Please, someone can help me ? Am I doing something wrong in the code ? let me know if you need some additional information.
Thanks in advance.

buffer question

Most helpful comment

It is not the same. Position 10??? I suggest you print the character sequence to a file, then use something like wdiff to compare the values or some similar tool. It is probably simpler than looking at a literal screenshot or asking someone else to do that for you.

All 3 comments

It is not the same. Position 10??? I suggest you print the character sequence to a file, then use something like wdiff to compare the values or some similar tool. It is probably simpler than looking at a literal screenshot or asking someone else to do that for you.

@AdamMajer thank you for your reply.

Yep, you're right, the conversion is not the same, but it shouldn't ? I'm using the same encode (ascii) to get the buffer.

I've changed the position 10 by myself, and the hash matches as expected.

Yep, you're right, the conversion is not the same, but it shouldn't ? I'm using the same encode (ascii) to get the buffer.

Yes, and you can’t encode “ã” as ASCII, that’s why that doesn’t work. Node gives you the Latin-1 encoding of ã instead, and C# gives you the (ASCII) encoding of ? (as a replacement character, like �).

This is not a bug, and not a problem with Node.js itself, so I’ll close this issue. For general questions about using Node.js, like this, nodejs/help may be a better place.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dfahlander picture dfahlander  ·  3Comments

fanjunzhi picture fanjunzhi  ·  3Comments

loretoparisi picture loretoparisi  ·  3Comments

mcollina picture mcollina  ·  3Comments

vsemozhetbyt picture vsemozhetbyt  ·  3Comments