Skip to content

Instantly share code, notes, and snippets.

@iKunalChhabra
Last active December 21, 2022 15:28
Show Gist options
  • Save iKunalChhabra/4ed3f35e592b0f738fb55cec9ee919ba to your computer and use it in GitHub Desktop.
Save iKunalChhabra/4ed3f35e592b0f738fb55cec9ee919ba to your computer and use it in GitHub Desktop.
snippet to compute hashes for various data types
import hashlib
import json
def get_hash(data, hash_type='md5', sort=True):
if not data:
raise ValueError('Data cannot be empty')
if hash_type not in hashlib.algorithms_available:
raise ValueError(f"Invalid hash type: {hash_type}\nAvailable hash types: {hashlib.algorithms_available}")
if isinstance(data, set):
data = list(data)
if (isinstance(data, list) or isinstance(data, tuple)) and sort:
data = sorted(data)
str_data = json.dumps(data, sort_keys=sort).encode('utf-8')
hash_func = getattr(hashlib, hash_type)
return hash_func(str_data).hexdigest()
# ############ Tests #############
def test_hash(data):
print(f"Data: {data}, Type: {type(data).__name__}")
print('md5 hash: ', get_hash(data, hash_type='md5'))
print('sha1 hash: ', get_hash(data, hash_type='sha1'))
print('sha256 hash: ', get_hash(data, hash_type='sha256'))
print('sha512 hash: ', get_hash(data, hash_type='sha512'))
print()
def test_hash_for_different_types():
data_types = [1, 1.0, 'Hello', True, [1, 2, 3], (1, 2, 3), {1, 2, 3}, {'a': 1, 'b': 2, 'c': 3}]
for data in data_types:
test_hash(data)
def test_equality(data1, data2):
print(f"Data1: {data1}, Type: {type(data1).__name__}")
print(f"Data2: {data2}, Type: {type(data2).__name__}")
if get_hash(data1) == get_hash(data2):
print('Hashes are equal')
else:
print('Hashes are not equal')
print()
def test_equality_for_different_values():
data1 = 1
data2 = 1
test_equality(data1, data2)
data1 = 1.0
data2 = 1.0
test_equality(data1, data2)
data1 = "Hello"
data2 = "Hello"
test_equality(data1, data2)
data1 = True
data2 = True
test_equality(data1, data2)
data1 = [1, 2, 3]
data2 = [1, 3, 2]
test_equality(data1, data2)
data1 = (1, 2, 3)
data2 = (1, 3, 2)
test_equality(data1, data2)
data1 = {1, 2, 3}
data2 = {1, 3, 2}
test_equality(data1, data2)
data1 = {'a': 1, 'b': 2, 'c': 3}
data2 = {'a': 1, 'c': 3, 'b': 2}
test_equality(data1, data2)
if __name__ == '__main__':
test_hash_for_different_types()
test_equality_for_different_values()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment