|
1 | | -#!/usr/bin/env python2 |
| 1 | +#!/usr/bin/env python3 |
2 | 2 | import binascii |
3 | 3 | import hashlib |
4 | 4 |
|
5 | 5 | import sha3 |
6 | 6 |
|
| 7 | + |
7 | 8 | def main(): |
8 | 9 | hash_funcs = [ |
9 | | - ('SHA-1', hashlib.sha1), |
10 | | - ('SHA-224', hashlib.sha224), |
11 | | - ('SHA-256', hashlib.sha256), |
12 | | - ('SHA-384', hashlib.sha384), |
13 | | - ('SHA-512', hashlib.sha512)] |
| 10 | + ("SHA-1", hashlib.sha1), |
| 11 | + ("SHA-224", hashlib.sha224), |
| 12 | + ("SHA-256", hashlib.sha256), |
| 13 | + ("SHA-384", hashlib.sha384), |
| 14 | + ("SHA-512", hashlib.sha512), |
| 15 | + ] |
14 | 16 |
|
15 | 17 | for (sha_type, sha_func) in hash_funcs: |
16 | | - digest = sha_func('abc').digest() |
| 18 | + digest = sha_func("abc".encode()).digest() |
17 | 19 |
|
18 | 20 | # Only loop 4 times since an iteration was done above |
19 | | - for x in xrange(0, 4): |
| 21 | + for x in range(0, 4): |
20 | 22 | digest = sha_func(digest).digest() |
21 | | - print('{} with 5 Rounds: {}'.format(sha_type, digest.encode('hex'))) |
| 23 | + print("{:>8} with 5 Rounds: {}".format(sha_type, binascii.hexlify(digest).decode())) |
22 | 24 |
|
23 | 25 | # Loop another 5 times to get to 10 |
24 | | - for x in xrange(0, 5): |
| 26 | + for x in range(0, 5): |
25 | 27 | digest = sha_func(digest).digest() |
26 | | - print('{} with 10 Rounds: {}'.format(sha_type, digest.encode('hex'))) |
| 28 | + print("{:>8} with 10 Rounds: {}".format(sha_type, binascii.hexlify(digest).decode())) |
27 | 29 |
|
28 | 30 | # SHA3 is handled differently as it's not in hashlib. |
29 | 31 | # Use the SHA3 creators' version |
30 | 32 | hash3_funcs = [ |
31 | | - ('SHA3-224', sha3.SHA3_224), |
32 | | - ('SHA3-256', sha3.SHA3_256), |
33 | | - ('SHA3-384', sha3.SHA3_384), |
34 | | - ('SHA3-512', sha3.SHA3_512)] |
| 33 | + ("SHA3-224", sha3.SHA3_224), |
| 34 | + ("SHA3-256", sha3.SHA3_256), |
| 35 | + ("SHA3-384", sha3.SHA3_384), |
| 36 | + ("SHA3-512", sha3.SHA3_512), |
| 37 | + ] |
35 | 38 |
|
36 | 39 | for (sha_type, sha_func) in hash3_funcs: |
37 | | - digest = sha_func([ord(c) for c in 'abc']) |
| 40 | + digest = sha_func([ord(c) for c in "abc"]) |
38 | 41 |
|
39 | 42 | # Only loop 4 times since an iteration was done above |
40 | | - for x in xrange(0, 4): |
| 43 | + for x in range(0, 4): |
41 | 44 | digest = sha_func(digest) |
42 | | - print('{} with 5 Rounds: {}'.format(sha_type, binascii.hexlify(digest))) |
| 45 | + print("{:>8} with 5 Rounds: {}".format(sha_type, binascii.hexlify(digest).decode())) |
43 | 46 |
|
44 | 47 | # Loop another 5 times to get to 10 |
45 | | - for x in xrange(0, 5): |
| 48 | + for x in range(0, 5): |
46 | 49 | digest = sha_func(digest) |
47 | | - print('{} with 10 Rounds: {}'.format(sha_type, binascii.hexlify(digest))) |
| 50 | + print("{:>8} with 10 Rounds: {}".format(sha_type, binascii.hexlify(digest).decode())) |
48 | 51 |
|
49 | | - hash3_funcs = [ |
50 | | - ('SHAKE128', sha3.SHAKE128), |
51 | | - ('SHAKE256', sha3.SHAKE256)] |
| 52 | + hash3_funcs = [("SHAKE128", sha3.SHAKE128), ("SHAKE256", sha3.SHAKE256)] |
52 | 53 |
|
53 | 54 | for (sha_type, sha_func) in hash3_funcs: |
54 | | - digest = sha_func([ord(c) for c in 'abc'], 31) |
| 55 | + digest = sha_func([ord(c) for c in "abc"], 31) |
55 | 56 |
|
56 | 57 | # Only loop 4 times since an iteration was done above |
57 | | - for x in xrange(0, 4): |
| 58 | + for x in range(0, 4): |
58 | 59 | digest = sha_func(digest, 31) |
59 | | - print('{} with 5 Rounds: {}'.format(sha_type, binascii.hexlify(digest))) |
| 60 | + print("{:>8} with 5 Rounds: {}".format(sha_type, binascii.hexlify(digest).decode())) |
60 | 61 |
|
61 | 62 | # Loop another 5 times to get to 10 |
62 | | - for x in xrange(0, 5): |
| 63 | + for x in range(0, 5): |
63 | 64 | digest = sha_func(digest, 31) |
64 | | - print('{} with 10 Rounds: {}'.format(sha_type, binascii.hexlify(digest))) |
| 65 | + print("{:>8} with 10 Rounds: {}".format(sha_type, binascii.hexlify(digest).decode())) |
| 66 | + |
65 | 67 |
|
66 | | -if ('__main__' == __name__): |
| 68 | +if "__main__" == __name__: |
67 | 69 | main() |
0 commit comments