Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a variable length hash

Tags:

python

hash

Is there a way in python to generate a short version of hashcode, SHA128 or SHA256 I tried this but its not correct way to get[extracting substring to shorten]

hashlib.sha256(str.encode('utf-8')).hexdigest()[:8]

for example if i have a string, i want 8 character hash

str = "iamtestingastringtogethash"
print(hashlib.sha256(str.encode('utf-8').hexdigest(),8))

Output: some_8char_hash

like image 814
codeexplorer Avatar asked Oct 19 '25 06:10

codeexplorer


1 Answers

Ok i found my own fix,

hashlib.shake_256(str.encode("utf-8")).hexdigest(length=8)
like image 199
codeexplorer Avatar answered Oct 21 '25 18:10

codeexplorer