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
|
import sys
import json
import unittest
import subprocess
sys.path.append('..')
import fhash
class TestFhash(unittest.TestCase):
# these files were teseted with openssl
HASHES = json.load(open('hashes.json'))
FHASH = "../fhash.py"
DIR = "files/"
def test_shattered(self):
"""Just for fun, two files that have the same hash, breaking sha1"""
SHATTERED = '38762cf7f55934b34d179ae6a4c80cadccbb7f0a'
cmd = [self.FHASH, 'sha1', '-i', self.DIR + 'shattered-1.pdf']
shat1 = subprocess.check_output(cmd).strip().decode()
cmd[3] = self.DIR + 'shattered-2.pdf'
shat2 = subprocess.check_output(cmd).strip().decode()
self.assertTrue(shat1 == shat2 == SHATTERED)
def test_file_types(self):
"""Tests that hashes for different file types are correct.
Uses sha1"""
for k in self.HASHES.keys():
cmd = [self.FHASH, 'sha1', '-i', self.DIR + k]
fh = subprocess.check_output(cmd).strip().decode()
ssl = self.HASHES[k]['sha1']
self.assertFalse(ssl != fh,
f"{k}:{fh} did not match the correct hash: {ssl}"
)
def test_algos(self):
"""Tests that hashes for different algorithms and sizes are correct.
"""
for k in self.HASHES.keys():
for alg in fhash.ALGOS:
if alg == 'md5' or alg == 'sha1':
cmd = [self.FHASH, alg, '-i', self.DIR + k]
fh = subprocess.check_output(cmd).strip().decode()
ssl = self.HASHES[k][alg]
self.assertFalse(ssl != fh,
f"{k}:{fh} did not match the correct hash: {ssl}"
)
else:
for s in fhash.SIZES:
cmd = [self.FHASH, alg, '-i', self.DIR + k, '-s',
str(s)]
fh = subprocess.check_output(cmd).strip().decode()
ssl = self.HASHES[k][alg + '-' + str(s)]
self.assertFalse(ssl != fh,
f"{k}:{fhash} did not match the correct hash: {ssl}"
)
if __name__ == '__main__':
unittest.main()
|