Hashing in SHA512 using a salt? - Python Hashing in SHA512 using a salt? - Python python python

Hashing in SHA512 using a salt? - Python


Samir's answer is correct but somewhat cryptic. Basically, the salt is just a randomly derived bit of data that you prefix or postfix your data with to dramatically increase the complexity of a dictionary attack on your hashed value. So given a salt s and data d you'd just do the following to generate a salted hash of the data:

import hashlibhashlib.sha512( s + d ).hexdigest()

See this wikipedia article for more details


Just add the salt to your sensitive data:

>>> import hashlib>>> m = hashlib.sha512()>>> m.update('salt')>>> m.update('sensitive data')>>> m.hexdigest()'70197a4d3a5cd29b62d4239007b1c5c3c0009d42d190308fd855fc459b107f40a03bd427cb6d87de18911f21ae9fdfc24dadb0163741559719669c7668d7d587'>>> n = hashlib.sha512()>>> n.update('%ssensitive data' % 'salt')>>> n.hexdigest()'70197a4d3a5cd29b62d4239007b1c5c3c0009d42d190308fd855fc459b107f40a03bd427cb6d87de18911f21ae9fdfc24dadb0163741559719669c7668d7d587'>>> hashlib.sha512('salt' + 'sensitive data').hexdigest()'70197a4d3a5cd29b62d4239007b1c5c3c0009d42d190308fd855fc459b107f40a03bd427cb6d87de18911f21ae9fdfc24dadb0163741559719669c7668d7d587'


Salting isn't a magical process that the library needs to help you with—it's just additional data provided to stop rainbow tables from working.

>>> import hashlib>>> m = hashlib.sha512()>>> m.update(b"Nobody inspects")>>> m.update(b" the spammish repetition")>>> m.digest()b'\xd0\xf4\xc1LH\xadH7\x90^\xa7R\x0c\xc4\xafp\x0fd3\xce\t\x85\xe6\xbb\x87\xb6\xb4a|\xb9D\xab\xf8\x14\xbdS\x96M\xdb\xf5[A\xe5\x81+:\xfe\x90\x89\x0c\nM\xb7\\\xb0Cg\xe19\xfdb\xea\xb2\xe1'>>> m.update(b"My super-secret salt.")>>> m.digest()b'\xcd\xd7K\xd9!~\xa8\x1d6\x9b\xa6\xde\x06\t\x02\xa1+}\xaeNA\x94a`\xaa\xf4\xe9\xb5\xff\x1f\x9cE\x84m\xbb\x98U\xb4z\x92\x9e\xe8\xc9\xc2\xc8\x8f\x068e\xb0\r\xed\xb7\xde\x80\xa6,\n\x111w{\xa2\x9b'