NodeJS:bcrypt与原生密码

有人能指出两者之间的差异,以及各自使用的示例情况吗?

bcrypt看起来不错。

使用bcrypt,你想要做慢和计算昂贵的散列 – 这通常是散列,你真的不希望攻击者能够颠倒散列,例如用户密码。 使用本机encryption的一切。

与@ mike-scott的答案一样,你应该更喜欢对密码相关的东西进行crypto ,但是你仍然可以使用crypto来执行各种任务,比如创build随机标记或HMAC校验和或者SHA1 / MD5散列:

 var crypto = require('crypto'); // random tokens var buf = crypto.randomBytes(16).toString('hex'); console.log('Random token of %d bytes in hexadecimal: %s', buf.length, buf); var buf = crypto.randomBytes(16).toString('base64'); console.log('Random token of %d bytes in base 64: %s', buf.length, buf); // a hashed message authentication checksum (HMAC) using a shared secret key var string = 'My coffee please'; var key = 'Right away sir'; var encrypted = crypto.createHmac('sha1', key).update(string).digest('hex'); console.log('Encrypting "%s" using passphrase "%s": %s', string, key, encrypted); // a MD5 hash var hashmd5 = crypto.createHash('md5').update(string).digest('hex'); console.log('The MD5 hash of "%s" is %s', string, hashmd5); // a SHA1 hash var hashsha1 = crypto.createHash('sha1').update(string).digest('hex'); console.log('The SHA1 hash of "%s" is %s', string, hashsha1);