在bcryptjs包中有一个hash(s,salt)方法。
/**
* Asynchronously generates a hash for the given string.
* @param s String to hash
* @param salt Salt length to generate or salt to use
* @return Promise with resulting hash, if callback has been omitted
*/
export declare function hash(s: string, salt: number | string): Promise<string>;使用数字salt参数是有意义的,但是如果盐是string会发生什么呢?我可以在这里使用任意的随机字符串吗?
发布于 2020-09-15 22:23:46
如果您查看示例in the package docs,您会发现salt字符串是由函数genSalt返回的值。你不能使用随机字符串(试一试,你会得到一个异常)。
数字不是字符串的长度,而是散列函数的成本因子--将其递增1将使计算散列的时间加倍。
下面是一些示例:
> var bcrypt = require('bcryptjs');
undefined
> bcrypt.genSaltSync(12)
'$2a$12$MDnofLJT8LrIILyh8SCle.'
> bcrypt.genSaltSync(14)
'$2a$14$fuc6ZCGfcUmsG.GiUYmdGe'
> bcrypt.hashSync("password", bcrypt.genSaltSync(12))
'$2a$12$NowrlsgseFUgTxlAUZ3jw.uZyf2uuZkeaoZU0r997DLd00/y0yp6e'
> bcrypt.hashSync("password", bcrypt.genSaltSync(15))
'$2a$15$xOjjGl6f60A3zUck6HhSEu/UcLLG//EkbDTKl6GFy3jNTgT..kQPC'
> bcrypt.hashSync("password", 12)
'$2a$12$Ks072IiTxgBYG9atJYeHCu7QpnIOylp/VjQmV6vW4mKRh43hYxkcO'
> bcrypt.hashSync("password", "invalid")
Uncaught Error: Invalid salt version: in
at _hash (/home/blah/blah/node_modules/bcryptjs/dist/bcrypt.js:1280:19)
at Object.bcrypt.hashSync (/home/blah/blah/node_modules/bcryptjs/dist/bcrypt.js:190:16)https://stackoverflow.com/questions/63859143
复制相似问题