Python(django)和Javascript(crypto.js)中的PBKDF2哈希值不一致

时间:2014-05-01 21:42:43

标签: javascript python django encryption cryptography

Django代码:

hasher = PBKDF2PasswordHasher()
print hasher.encode(u'qwerty', u'qwerty', iterations=1).split('$')[3]
# T9t/EJXFpPs2Lhca7IVGphTC/OdEloPMHw1UhDnXcyQ=
# hex: 4fdb7f1095c5a4fb362e171aec8546a614c2fce7449683cc1f0d548439d77324

Javascript代码:

var key = CryptoJS.PBKDF2('qwerty', 'qwerty', { iterations: 1, keySize: 256/32 });
console.log(key.toString(CryptoJS.enc.Base64));
// vfBGfG30ahsx6QWs9vrdTGV+wmlg1d5f2+5bFnP5SAU=
// hex: bdf0467c6df46a1b31e905acf6fadd4c657ec26960d5de5fdbee5b1673f94805

1 个答案:

答案 0 :(得分:3)

我没有在JS中指定正确的哈希。默认值是CryptoJS的SHA1哈希和Django的SHA526哈希。

CryptoJS.PBKDF2('qwerty', 'qwerty', { iterations: 1, keySize: 256/32, hasher: CryptoJS.algo.SHA256 });

修复了问题