Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hash string size

Tags:

python

sha

hmac

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.

like image 790
synergetic Avatar asked Apr 02 '12 09:04

synergetic


2 Answers

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).

like image 177
Y.H Wong Avatar answered Oct 17 '22 17:10

Y.H Wong


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);
like image 42
A_A Avatar answered Oct 17 '22 17:10

A_A