30 seconds of code

  Back to Home

hashNode

nodeutilitypromiseintermediate

Creates a hash for a value using the SHA-256 algorithm. Returns a promise.

Use crypto API to create a hash for the given value, setTimeout to prevent blocking on a long operation, and a Promise to give it a familiar interface.

const crypto = require('crypto');
const hashNode = val =>
  new Promise(resolve =>
    setTimeout(
      () =>
        resolve(
          crypto
            .createHash('sha256')
            .update(val)
            .digest('hex')
        ),
      0
    )
  );