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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With