Kor*_*ory 18 cryptography bcrypt password-hash node.js
我已经尝试了几天在我的Windows机器上安装bcrypt而没有运气.其中一个依赖项(Windows 7 SDK)不希望安装,即使我已尝试过网络上的许多建议,它只是拒绝合作.
我需要一个很好的替代bcrypt,它没有任何依赖.
end*_*cat 20
查看https://npmjs.org/package/bcryptjs,它与bcrypt完全兼容,没有依赖项.
或者https://npmjs.org/package/simplecrypt如果你不想要加密样板,只需要加密和解密字符串.
mal*_*ala 13
截至2020 年 4 月 24 日scrypt,crypto模块中使用了一种很好的内置散列密码方式
// Using the built in crypto module
const { scryptSync, randomBytes } = require("crypto");
// Any random string here (ideally should be atleast 16 bytes)
const salt = randomBytes(16).toString("hex")
// Pass the password string and get hashed password back
// ( and store only the hashed string in your database)
const getHash = (password) => scryptSync(password, salt, 32).toString("hex");
Run Code Online (Sandbox Code Playgroud)
Shi*_*vam 12
这是 @malik-bagwala 的改进版本,带有 JsDocs、类型和匹配密码功能。
import { randomBytes, scryptSync } from 'crypto';
// Pass the password string and get hashed password back
// ( and store only the hashed string in your database)
const encryptPassword = (password: string, salt: string) => {
return scryptSync(password, salt, 32).toString('hex');
};
/**
* Hash password with random salt
* @return {string} password hash followed by salt
* XXXX till 64 XXXX till 32
*
*/
export const hashPassword = (password: string): string => {
// Any random string here (ideally should be at least 16 bytes)
const salt = randomBytes(16).toString('hex');
return encryptPassword(password, salt) + salt;
};
// fetch the user from your db and then use this function
/**
* Match password against the stored hash
*/
export const matchPassword = (password: string, hash: string): Boolean => {
// extract salt from the hashed string
// our hex password length is 32*2 = 64
const salt = hash.slice(64);
const originalPassHash = hash.slice(0, 64);
const currentPassHash = encryptPassword(password, salt);
return originalPassHash === currentPassHash;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16614 次 |
| 最近记录: |