1

In PHP, the code below returns the raw output of the SHA1 of the "string"

sha1("string", true);

What is the nodeJS equivalent of getting the SHA1 raw output?

Edit: I made some test and this line:

crypto.createHash('sha1').update('string').digest('base64');

generates same output as php's

base64_encode(sha1('string', true));

My issue occurs when I try to concatenate a string and the result of sha1, the get the sha1 again:

base64_encode(sha1(sha1("string", true) . "another string", true))

Different with nodejs:

var stringhash = crypto.createHash('sha1').update('string').digest();
crypto.createHash('sha1').update("another string" + stringhash).digest('base64')

1 Answer 1

4

Something like this:

const crypto = require('crypto');
let digest   = crypto.createHash('sha1').update('string').digest();
process.stdout.write( digest );

EDIT: the equivalent of your second example:

let hash1  = crypto.createHash('sha1').update('string').digest();
let hash2  = crypto.createHash('sha1').update(hash1).update('another string');
let digest = hash2.digest('base64');
Sign up to request clarification or add additional context in comments.

2 Comments

Based on nodejs documentation, in .digest(): "If encoding is provided a string will be returned; otherwise a Buffer is returned." "Buffer" is the equivalent of the raw output of PHP's sha1?
@whoknows a Buffer in Node.js contains binary ("raw") data in the form of a UInt8Array, which is likely equivalent to what the PHP code returns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.