Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js, how do I create a sha512 hash asynchronously?

var crypto = require('crypto');
var sha = crypto.createHash('sha512').update(String(s));
var result = sha.digest('hex');

That's my current code.

How do I do this async? I'm planning to do the sha512 100,000 times.

like image 983
TIMEX Avatar asked Aug 09 '14 07:08

TIMEX


People also ask

How do I create a SHA512 hash in node JS?

To create a hash from strings you just need a few lines in nodejs: // generate a hash from string var crypto = require('crypto'), text = 'hello bob', key = 'mysecret key' // create hahs var hash = crypto. createHmac('sha512', key) hash. update(text) var value = hash.

How do I create asynchronous in node JS?

The asynchronous function can be written in Node. js using 'async' preceding the function name. The asynchronous function returns implicit Promise as a result. The async function helps to write promise-based code asynchronously via the event-loop.

How does node js work asynchronously?

NodeJS is an asynchronous event-driven JavaScript runtime environment designed to build scalable network applications. Asynchronous here refers to all those functions in JavaScript that are processed in the background without blocking any other request.


1 Answers

Node's crypto module does not provide asynchronous SHA512 hashing at present, and although the createHash() stream interface looks asynchronous it will also execute in the main thread and block the event loop.

There is an issue open for this: https://github.com/nodejs/node/issues/678

In the interim, you can use @ronomon/crypto-async to do SHA512 asynchronously and concurrently in the threadpool without blocking the event loop, for multi-core throughput.

like image 105
Joran Greef Avatar answered Sep 21 '22 17:09

Joran Greef