Skip to content

Instantly share code, notes, and snippets.

@nberlette
Created May 14, 2021 20:45
Show Gist options
  • Save nberlette/e62a031a0c37084ab018f5930ba7d0ce to your computer and use it in GitHub Desktop.
Save nberlette/e62a031a0c37084ab018f5930ba7d0ce to your computer and use it in GitHub Desktop.
Quick encryption module for node. Shoutout @feross
/*!
MIT License.
Algorithms compiled by Nicholas Berlette <https://berlette.com>,
originally by Feross Aboukhadijeh <https://feross.org/opensource>
*/
const crypto = require('crypto')
const allowedMethods = ['sha1', 'sha256', 'md5']
async function hash (method = 'sha256', buf, cb = null) {
if (typeof method === 'string' && allowedMethods.includes(method)) {
const hash = hashSync(method, buf)
process.nextTick(function () {
return cb(hash)
})
}
}
function hashSync (method = 'sha256', buf) {
return crypto.createHash(method)
.update(buf)
.digest('hex')
}
module.exports = hash
module.exports.sync = hashSync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment