简体   繁体   English

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

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

I want to create password hashes when creating users and store them to my database. 我想在创建用户时创建密码哈希并将其存储到我的数据库中。 My database password column stores hashes of 64 chars ( nvarchar(64) ). 我的数据库密码列存储的哈希值为64个字符( nvarchar(64) )。 My hash configuration: 我的哈希配置:

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';

When hashing the users plain text password I use this function 当哈希用户纯文本密码时,我使用此功能

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;
}

Unfortunately the hash function returns a password hash with 128 characters. 不幸的是,哈希函数返回具有128个字符的密码哈希。 Is it possible to define a hash length or do I always get returned a 128 characters long hash? 是否可以定义哈希长度,还是总是返回128个字符长的哈希?

Parameter keylen ( length in your snippet) specifies number of bytes in the returned buffer. 参数keylen (代码段中的length )指定返回缓冲区中的字节数。 Converting this buffer to a hex string doubles the returned string, because each byte is represented by two characters. 将此缓冲区转换为hex字符串会使返回的字符串加倍,因为每个字节都由两个字符表示。

If you want to get a hex string of 64 chars, then you must set length = 32 . 如果要获取64个字符的hex字符串,则必须设置length = 32

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

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