Created
May 14, 2021 20:45
-
-
Save nberlette/e62a031a0c37084ab018f5930ba7d0ce to your computer and use it in GitHub Desktop.
Quick encryption module for node. Shoutout @feross
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
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