Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing in SHA512 using a salt? - Python

I have been looking through ths hashlib documentation but haven't found anything talking about using salt when hashing data.

Help would be great.

like image 932
RadiantHex Avatar asked May 24 '10 16:05

RadiantHex


2 Answers

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 hashlib hashlib.sha512( s + d ).hexdigest() 

See this wikipedia article for more details

like image 186
Rakis Avatar answered Oct 05 '22 19:10

Rakis


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' 
like image 29
Jason Leveille Avatar answered Oct 05 '22 19:10

Jason Leveille