The Passlib Password Hash interface either lets you set the salt size, or the salt
value itself. From the documentation on pbkdf2_sha256
:
salt
(bytes) Optional salt bytes. If specified, the length must be between 0-1024 bytes. If not specified, a 16 byte salt will be autogenerated (this is recommended).
salt_size
(int) – Optional number of bytes to use when autogenerating new salts. Defaults to 16 bytes, but can be any value between 0 and 1024.
so you can set your own pre-generated salt:
>>> from passlib.hash import pbkdf2_sha256
>>> pbkdf2_sha256.hash("password", rounds=200000, salt=b'spamhameggs')
'$pbkdf2-sha256$200000$c3BhbWhhbWVnZ3M$WL9OLVcb3f7HqHeNT./kCJeunydLCi4JykzEuAdewcI'
However, note that the salt is part of the returned string. The string contains not only the resulting hash, but also the algorithm, the number of rounds used and the salt used, delimited by $
. The salt is encoded with a modified form of base64. You can verify this by decoding the string c3BhbWhhbWVnZ3M
again::
>>> from passlib.utils.binary import ab64_decode
>>> ab64_decode(b'c3BhbWhhbWVnZ3M')
b'spamhameggs'
See the Format & Algorithm section for the pbkdf2_sha256
docs.
So when you store the full string pbkdf2_sha256
in the database, everything to validate the string is right there in the value, including the salt. Leaving generating a random salt is best left to that library as it'll use a secure method to generate one.
You may want to read the Passlib tutorial on password hashing, which includes coverage of how to hash passwords when storing in the database, and how to verify them again (e.g. using pdkdf2_sha256.verify(password_user_entered, hash_stored_in_database)
), which covers exactly this ground.
hashlib.pbkdf2_hmac()
takes a salt. What function are you using? – Geophysics$<type>$rounds=<rounds>$salt$hash
, so if you want to store the hash separately you can if you want to. – Geophysicssalt
argument is accepted.pbkdf2_sha256.encrypt("password", rounds=200000, salt='blabla235')
– Geophysicsb'blabla235'
. No,salt_size
determines how big a salt is generated for you. It is not the same thing assalt
. – Geophysics