summaryrefslogtreecommitdiffstats
path: root/test/genHashRounds.py
blob: 12f3845f8f533b77d73bbd8d9f6e621d232cf025 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python2
import binascii
import hashlib

import sha3

def main():
    hash_funcs = [
        ('SHA-1', hashlib.sha1),
        ('SHA-224', hashlib.sha224),
        ('SHA-256', hashlib.sha256),
        ('SHA-384', hashlib.sha384),
        ('SHA-512', hashlib.sha512)]

    for (sha_type, sha_func) in hash_funcs:
        digest = sha_func('abc').digest()

        # Only loop 4 times since an iteration was done above
        for x in xrange(0, 4):
            digest = sha_func(digest).digest()
        print('{} with 5 Rounds: {}'.format(sha_type, digest.encode('hex')))

        # Loop another 5 times to get to 10
        for x in xrange(0, 5):
            digest = sha_func(digest).digest()
        print('{} with 10 Rounds: {}'.format(sha_type, digest.encode('hex')))

    # SHA3 is handled differently as it's not in hashlib.
    # Use the SHA3 creators' version
    hash3_funcs = [
        ('SHA3-224', sha3.SHA3_224),
        ('SHA3-256', sha3.SHA3_256),
        ('SHA3-384', sha3.SHA3_384),
        ('SHA3-512', sha3.SHA3_512)]

    for (sha_type, sha_func) in hash3_funcs:
        digest = sha_func([ord(c) for c in 'abc'])

        # Only loop 4 times since an iteration was done above
        for x in xrange(0, 4):
            digest = sha_func(digest)
        print('{} with 5 Rounds: {}'.format(sha_type, binascii.hexlify(digest)))

        # Loop another 5 times to get to 10
        for x in xrange(0, 5):
            digest = sha_func(digest)
        print('{} with 10 Rounds: {}'.format(sha_type, binascii.hexlify(digest)))

    hash3_funcs = [
        ('SHAKE128', sha3.SHAKE128),
        ('SHAKE256', sha3.SHAKE256)]

    for (sha_type, sha_func) in hash3_funcs:
        digest = sha_func([ord(c) for c in 'abc'], 31)

        # Only loop 4 times since an iteration was done above
        for x in xrange(0, 4):
            digest = sha_func(digest, 31)
        print('{} with 5 Rounds: {}'.format(sha_type, binascii.hexlify(digest)))

        # Loop another 5 times to get to 10
        for x in xrange(0, 5):
            digest = sha_func(digest, 31)
        print('{} with 10 Rounds: {}'.format(sha_type, binascii.hexlify(digest)))

if ('__main__' == __name__):
    main()