Looking to store usernames and passwords in a database, and am wondering what the safest way to do so is. I know I have to use a salt somewhere, but am not sure how to generate it securely or how to apply it to encrypt the password. Some sample Python code would be greatly appreciated. Thanks.
Looking to store usernames and passwords in a database, and am wondering what the
Share
Store the password+salt as a hash and the salt. Take a look at how Django does it: basic docs and source.
In the db they store
<type of hash>$<salt>$<hash>in a single char field. You can also store the three parts in separate fields.The function to set the password:
The get_hexdigest is just a thin wrapper around some hashing algorithms. You can use hashlib for that. Something like
hashlib.sha1('%s%s' % (salt, hash)).hexdigest()And the function to check the password: