This repository was archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·120 lines (85 loc) · 3.31 KB
/
Copy pathtest.py
File metadata and controls
executable file
·120 lines (85 loc) · 3.31 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
from __future__ import print_function
# Standard library imports
import unittest
import random
import hashlib
try:
range = xrange
except NameError:
pass
# Custom SHA-1 library
import sha1
class TestSha1(unittest.TestCase):
"""TestSha1 class
Test case for the custom SHA-1 implementation.
"""
def test_similar(self):
"""Test Similar SHA-1 Inputs
Tests sets of messages with 1 bit of difference. Ensures that all
messages produce unique hashes.
"""
print('\n>>> running: test_similar')
first_msg = bytearray(get_random_bytes())
modified_msg = bytearray()
# Pick a random byte, modify it by one bit
byte_to_modify = random.randrange(0, len(first_msg))
for i, byte in enumerate(first_msg):
augmentor = 1 if i == byte_to_modify else 0
modified_msg.append(byte + augmentor)
first_digest = sha1.sha1(bytes(first_msg))
modified_digest = sha1.sha1(bytes(modified_msg))
print('... test_similar: checking digest differences')
self.assertNotEqual(first_digest, modified_digest)
print('... test_similar: success')
def test_repeatable(self):
"""Test SHA-1 Repeatability
Runs the SHA-1 hashing function multiple times to ensure the same
outcome for any identical message input.
"""
print('\n>>> running: test_repeatable')
msg = bytearray(get_random_bytes())
first_digest = sha1.sha1(bytes(msg))
second_digest = sha1.sha1(bytes(msg))
print('... test_repeatable: checking for identical digests')
self.assertEqual(first_digest, second_digest)
print('... test_repeatable: success')
def test_comparison(self):
"""Test SHA-1 Library Accuracy
Runs the custom SHA-1 hashing function implementation with other
SHA-1 functions contained in the Python hashlib library.
"""
print('\n>>> running: test_comparison')
msg = bytearray(get_random_bytes())
custom_sha1_digest = sha1.sha1(bytes(msg))
stdlib_sha1_digest = hashlib.sha1(bytes(msg)).hexdigest()
print('... test_comparison: checking for identical digests')
self.assertEqual(custom_sha1_digest, stdlib_sha1_digest)
print('... test_comparison: success')
def test_associativity(self):
"""Test SHA-1 associativity
Tests the fact that sha1(ab) is equivalent to sha1(a) updated with b.
"""
print('\n>>> running: test_associativity')
msg1 = bytearray(get_random_bytes())
msg2 = bytearray(get_random_bytes())
first_digest = sha1.sha1(bytes(msg1) + bytes(msg2))
sha = sha1.Sha1Hash()
sha.update(msg1)
sha.update(msg2)
second_digest = sha.hexdigest()
print('... test_associativity: checking for identical digests')
self.assertEqual(first_digest, second_digest)
print('... test_associativity: success')
def get_random_bytes():
"""Get Random Bits
Generates a sequence of random bits of a random size between 1 and 1000
bits in the sequence.
Returns:
A stream of random bits.
"""
size = random.randrange(1, 1000)
for _ in range(size):
yield random.getrandbits(8)
if __name__ == '__main__':
unittest.main()