Search code examples
pythoncryptojspbkdf2passlib

How to limit key length with Passlib 1.7+


A server protocol requires me to derive a password hash with a limited key size. This is the given JavaScript + CryptoJS implementation:

var params = {keySize: size/32, hasher: CryptoJS.algo.SHA512, iterations: 5000}
var output = CryptoJS.PBKDF2(password, salt, params).toString();

I want to re-implement this in Python using Passlib, i.e. something like

from passlib.hash import pkbdf2_sha512
output = pbkdf2_sha512.hash(password, salt=salt, rounds=5000)

The Passlib API does not allow me to specify the key size. How to do it though?


Solution

  • If the derived key it to long just truncate it to the required length. Each byte is just as valid as every other byte, it makes no difference which bytes you use, there is no ordering.