In python I use the following code to create hash for user passwords:
self.password = hmac.new(security_key, raw_password, sha1).hexdigest()
Now I'd like to save this value to database. What size must be my database column? It seems related to digest_size property, but do not know what object or class has such property. sha1 doesn't have one.
The output of hmac is dependent on the hashing algorithm it uses. In your case, it uses sha1, which always output a 20-byte long byte string according to RFC2104. Calling hexdigest() turns the byte string into a printable hex format. 1 byte = 2 hex numbers, so the total is 40 hex characters. You can safely set your database column to char(40).
The output of the sha-1 hash function will be 20 characters long. (According to RFC2104, referenced by the hmac python module docs: "...L the byte-length of hash outputs (L=16 for MD5, L=20 for SHA-1).")
You can also verify this by:
import hashlib;
H = hashlib.sha1("blahblah");
print(H.digest_size);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With