前端用PBKDF2加密密码时为什么结果和后端对不上?
我在前端用Web Crypto API实现PBKDF2加盐哈希,但生成的密钥和后端Python的结果完全不一样。明明盐值、迭代次数、密钥长度都一样,是不是哪里调用错了?
我试过把salt转成Uint8Array,也确认了都是UTF-8编码,但还是不行。前端代码大概长这样:
const salt = new TextEncoder().encode('mysalt123');
const password = new TextEncoder().encode('mypassword');
const keyMaterial = await window.crypto.subtle.importKey(
'raw',
password,
{ name: 'PBKDF2' },
false,
['deriveBits']
);
const derivedKey = await window.crypto.subtle.deriveBits(
{
name: 'PBKDF2',
salt: salt,
iterations: 100000,
hash: 'SHA-256'
},
keyMaterial,
256
);
console.log(new Uint8Array(derivedKey));
后端用的是hashlib.pbkdf2_hmac('sha256', password, salt, 100000),结果却对不上,到底差在哪?
暂无解答