Skip to content

Commit 1f37809

Browse files
committed
Convert Python files to Python 3
1 parent 766f8ff commit 1f37809

File tree

2 files changed

+63
-52
lines changed

2 files changed

+63
-52
lines changed

test/genHashRounds.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python3
22
import binascii
33
import hashlib
44

55
import sha3
66

7+
78
def main():
89
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+
]
1416

1517
for (sha_type, sha_func) in hash_funcs:
16-
digest = sha_func('abc').digest()
18+
digest = sha_func("abc".encode()).digest()
1719

1820
# Only loop 4 times since an iteration was done above
19-
for x in xrange(0, 4):
21+
for x in range(0, 4):
2022
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()))
2224

2325
# Loop another 5 times to get to 10
24-
for x in xrange(0, 5):
26+
for x in range(0, 5):
2527
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()))
2729

2830
# SHA3 is handled differently as it's not in hashlib.
2931
# Use the SHA3 creators' version
3032
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+
]
3538

3639
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"])
3841

3942
# Only loop 4 times since an iteration was done above
40-
for x in xrange(0, 4):
43+
for x in range(0, 4):
4144
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()))
4346

4447
# Loop another 5 times to get to 10
45-
for x in xrange(0, 5):
48+
for x in range(0, 5):
4649
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()))
4851

49-
hash3_funcs = [
50-
('SHAKE128', sha3.SHAKE128),
51-
('SHAKE256', sha3.SHAKE256)]
52+
hash3_funcs = [("SHAKE128", sha3.SHAKE128), ("SHAKE256", sha3.SHAKE256)]
5253

5354
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)
5556

5657
# Only loop 4 times since an iteration was done above
57-
for x in xrange(0, 4):
58+
for x in range(0, 4):
5859
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()))
6061

6162
# Loop another 5 times to get to 10
62-
for x in xrange(0, 5):
63+
for x in range(0, 5):
6364
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+
6567

66-
if ('__main__' == __name__):
68+
if "__main__" == __name__:
6769
main()

test/genShake.py

100644100755
Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python3
22
import binascii
33

44
import sha3
55

6+
67
def main():
78
test_vectors = {
8-
'Short': 'abc',
9-
'Medium': 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',
10-
'Long': 'a' * 1000000
9+
"Short": "abc",
10+
"Medium": "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
11+
"Long": "a" * 1000000,
1112
}
12-
13-
hash_funcs = {'SHAKE128': sha3.SHAKE128, 'SHAKE256': sha3.SHAKE256}
13+
14+
hash_funcs = {"SHAKE128": sha3.SHAKE128, "SHAKE256": sha3.SHAKE256}
1415
output_lens = [31, 63]
15-
16+
1617
for hash_name, hash_func in hash_funcs.items():
1718
for vector_name, vector_value in test_vectors.items():
1819
for output_len in output_lens:
19-
print('%s with %s UTF-8 Input and %d bit Output: %s' % (
20-
hash_name,
21-
vector_name,
22-
output_len * 8,
23-
binascii.hexlify(hash_func(vector_value.encode(), output_len)).decode()
20+
print(
21+
"{} with {:>6} UTF-8 Input and {} bit Output: {}".format(
22+
hash_name,
23+
vector_name,
24+
output_len * 8,
25+
binascii.hexlify(hash_func(vector_value.encode(), output_len)).decode(),
2426
)
2527
)
26-
print('%s with %s UTF-16BE Input and %d bit Output: %s' % (
27-
hash_name,
28-
vector_name,
29-
output_len * 8,
30-
binascii.hexlify(hash_func(vector_value.encode('UTF-16BE'), output_len)).decode()
28+
print(
29+
"{} with {:>6} UTF-16BE Input and {} bit Output: {}".format(
30+
hash_name,
31+
vector_name,
32+
output_len * 8,
33+
binascii.hexlify(
34+
hash_func(vector_value.encode("UTF-16BE"), output_len)
35+
).decode(),
3136
)
3237
)
33-
print('%s with %s UTF-16LE Input and %d bit Output: %s' % (
34-
hash_name,
35-
vector_name,
36-
output_len * 8,
37-
binascii.hexlify(hash_func(vector_value.encode('UTF-16LE'), output_len)).decode()
38+
print(
39+
"{} with {:>6} UTF-16LE Input and {} bit Output: {}".format(
40+
hash_name,
41+
vector_name,
42+
output_len * 8,
43+
binascii.hexlify(
44+
hash_func(vector_value.encode("UTF-16LE"), output_len)
45+
).decode(),
3846
)
3947
)
4048

41-
if ('__main__' == __name__):
49+
50+
if "__main__" == __name__:
4251
main()

0 commit comments

Comments
 (0)