python - Hashing a time-string: Why not receive same results? -
why following code not generate @ least few identical md5 strings:
import hashlib import time hasher = hashlib.md5() def calchash(): localtime = time.localtime() timestring = time.strftime("%y-%m-%d-%h-%m-%s", localtime) print(timestring) hasher.update(timestring.encode('utf-8')) print("calculated: " + hasher.hexdigest()) in range(1,10): calchash()
i feeding not time stamp, generated string hasher. i'd expect identical md5 hashes if feed same string twice hasher.
2015-04-26-09-50-24 calculated: 52cae4a4231c812e5b79102a55721282 2015-04-26-09-50-24 calculated: 0329298a8a18246fc1fc2f9878252dcf 2015-04-26-09-50-24 calculated: 3db4562ca628a76c863f1308b8c41b04 2015-04-26-09-50-24 calculated: 51c482a637405897cd5d91f2145e424f 2015-04-26-09-50-24 calculated: 297eb85857fc85533a785fb13c200bdc 2015-04-26-09-50-24 calculated: 4288a660c70ee9ed40a8e7611176af91 2015-04-26-09-50-24 calculated: 4b998d607dba97578447d21cd82f6f33 2015-04-26-09-50-24 calculated: 45b93b13df62be5e3616ee89b7e803cc 2015-04-26-09-50-24 calculated: 0dbce249b10195b2a28b1825032e455c
you not resetting hasher. therefore, subsequent hashes hashes of accumulated strings.
change to:
def calchash(): hasher = hashlib.md5() localtime = time.localtime() timestring = time.strftime("%y-%m-%d-%h-%m-%s", localtime) print(timestring) hasher.update(timestring.encode('utf-8')) print("calculated: " + hasher.hexdigest())
Comments
Post a Comment