-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathhash_chain.py
49 lines (31 loc) · 1.04 KB
/
hash_chain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
# https://en.wikipedia.org/wiki/Hash_chain
import hashlib
def get_hash(text):
sha256 = hashlib.sha256(text.encode("utf-8"))
return sha256.hexdigest()
def hash_chain(text, number=1):
for _ in range(number):
text = get_hash(text)
return text
if __name__ == "__main__":
text = "Hello World!"
print("Text:", text)
# A hash chain is a successive application of a cryptographic hash function h to a string x x.
# For example, h(h(h(h(x)))) gives a hash chain of length 4, often denoted h^{4}(x)
h = get_hash
print("hash chain (5):", h(h(h(h(h(text))))))
print("\n")
print("hash_chain")
hash_text = hash_chain(text)
print("hash_chain(1):", hash_text)
print()
hash_text = hash_chain(text, number=4)
print("hash_chain(4):", hash_text)
hash_text = hash_chain(hash_text)
print("hash_chain(4+1):", hash_text)
print()
hash_text = hash_chain(text, number=5)
print("hash_chain(5):", hash_text)