繁体   English   中英

pbkdf2返回128个字符而不是64个字符的哈希

[英]pbkdf2 returns a hash with 128 characters instead of 64

我想在创建用户时创建密码哈希并将其存储到我的数据库中。 我的数据库密码列存储的哈希值为64个字符( nvarchar(64) )。 我的哈希配置:

const byteAmount: number = Number(process.env.BYTE_AMOUNT) || 16;
const iterations: number = Number(process.env.ITERATIONS) || 100;
const length: number = Number(process.env.LENGTH) || 64;
const algorithm: string = process.env.ALGORITHM || 'sha512';
const conversion: string = process.env.CONVERSION || 'Hex';

当哈希用户纯文本密码时,我使用此功能

public hashPassword = (password: BinaryLike, passwordSalt: BinaryLike): string => {
    const { iterations, length, algorithm }: { iterations: number, length: number, algorithm: string } = passwordConfig;
    const passwordHash: string = pbkdf2Sync(password, passwordSalt, iterations, length, algorithm).toString(passwordConfig.conversion);
    return passwordHash;
}

不幸的是,哈希函数返回具有128个字符的密码哈希。 是否可以定义哈希长度,还是总是返回128个字符长的哈希?

参数keylen (代码段中的length )指定返回缓冲区中的字节数。 将此缓冲区转换为hex字符串会使返回的字符串加倍,因为每个字节都由两个字符表示。

如果要获取64个字符的hex字符串,则必须设置length = 32

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM