def test_multi(self): # test all hashing functions algo = "md5" h1 = hashit.hashFile(FILE, hashit.new(algo), True) h2 = hashit.hashFile(FILE, hashit.new(algo)) h3 = hashit.hashIter(hashit.blockIter(open(FILE, "rb")), hashit.new(algo)) # just checking d = hashit.detect(hashit.hashFile(FILE, hashit.new("sha224"), False), hashit.generate_data_set("HALLO", hashit.__algorithms__, hashit.new)) self.assertEqual(d.certain[0] if d.certain else d.maybe[0], "sha224") self.assertTrue(h1 == h2 == h3 == FILE_SUM)
def test_hasher(self): file = open(FILE, "rb") data = file.read() for algo in hashit.__algorithms__: h1 = hashit.new(algo, data).hexdigest() h2 = hashit.hashFile(FILE, hashit.new(algo), True) self.assertEqual(h1, h2) self.assertEqual(h1, FILE_SUMS[algo]) h1 = hashit.new("md5", data).hexdigest() self.assertEqual(h1, FILE_SUM) file.close()
def test_detect(self): # generate data set ds = hashit.generate_data_set("Hallo", hashit.__algorithms__, hashit.new) # hash file three times h1 = hashit.hashIter(hashit.blockIter(open(FILE, "rb")), hashit.new("md5")) h2 = hashit.hashIter(hashit.blockIter(open(FILE, "rb")), hashit.new("sha224")) h3 = hashit.hashIter(hashit.blockIter(open(FILE, "rb")), hashit.new("sha1")) h4 = hashit.hashIter(hashit.blockIter(open(FILE, "rb")), hashit.new("crc32")) # detect for three algorigthms cl1 = hashit.detect(h1, ds) cl2 = hashit.detect(h2, ds) cl3 = hashit.detect(h3, ds) cl4 = hashit.detect(h4, ds) # correct hash names correct1 = "md5" correct2 = "sha224" correct4 = "crc32" # md5 or md4 self.assertTrue(correct1 in cl1.certain or correct1 in cl1.maybe) # only one left should be true self.assertTrue(correct2 in (cl2.certain if cl2.certain else cl2.maybe)) self.assertTrue(correct4 in cl4.certain) # and if it is to check hash with it self.assertEqual(hashit.new(correct2, b'Hallo').hexdigest(), hashit.new(cl2.certain[0] if cl2.certain else cl2.maybe[0], b'Hallo').hexdigest()) # for sha1 more options should be avaible self.assertTrue(len(cl3.certain) >= 1) # and work self.assertEqual(h3, hashit.hashIter(hashit.blockIter(open(FILE, "rb")), hashit.new(cl3.certain[0])))
def collision(): crc = lambda d=b'': hashit.new("crc32", d).hexdigest() done = {} for n in range(1000**3 * 4 + 1): n = str(n) h = crc(n.encode()) if h in done: print("ERROR collision found in CRC32", h, n, "and", done[h]) else: try: done[h] = n except MemoryError: done.clear()
def test_load(self): hashit.load(load_api_1) h1 = hashit.new("hash1", b'data') self.assertEqual(hex(h1.digest()), h1.hexdigest())
def test_load_all(self): hashit.load_all([load_api_2, load_api_3]) h2 = hashit.new("hash2", b'data') h3 = hashit.new("hash3", b'data') self.assertEqual(hex(h2.digest()), h2.hexdigest()) self.assertEqual(hex(h3.digest()), h3.hexdigest())
def easy_hashfile(file, algo): return easy_hash(file, hashit.new(algo))
def slow_hashfile(file, algo): return hashit.new(algo, open(file, "rb").read()).hexdigest()
def hashstr(string, algo): return hashit.new(algo, string.encode()).hexdigest()
def hashfile(file, algo): return hashit.hashIter(hashit.blockIter(open(file, "rb")), hashit.new(algo))
def main_(): COMMANDS = [ "hash an file", "hash files from a directory", "hash all files and folders in a directory", "check a checksum file", "help", "exit" ] command = gui.choicebox("Select command:", "HASHIT", COMMANDS) if command == COMMANDS[0]: filename = gui.fileopenbox("Choose a file to hash", "HASHIT") hashres = hashFile(filename, new(selecthash()), False) file = writetofile() gui.msgbox(hashres, "HASHIT") elif command == COMMANDS[1]: my_path = gui.diropenbox("select directory:", "HASHIT") files = [ my_path + "/" + f for f in os.listdir(my_path) if os.path.isfile(os.path.join(my_path, f)) ] files_to_hash = gui.multchoicebox("Select files to hash:", "HASHIT", files) hasher = selecthash() HASHED = [] for fname in files_to_hash: HASHED.append( str(hashFile(fname, new(hasher), False)) + " " + fname) file = writetofile() if file: open(file, "w").write("\n".join(HASHED)) else: gui.msgbox('\n\n'.join(HASHED)) elif command == COMMANDS[2]: my_path = gui.diropenbox("select directory:", "HASHIT") files = walk(my_path) hasher = selecthash() HASHED = [] for fname in files: HASHED.append( str(hashFile(fname, new(hasher), False)) + " " + fname) file = writetofile() if file: open(file, "w").write("\n".join(HASHED)) else: gui.msgbox('\n\n'.join(HASHED)) elif command == COMMANDS[3]: file = readfromfile() hasher = new(selecthash()) DONE = [] for c in check_(file, hasher, open(file, "r").readline(), False, False, False): if isinstance(c, str): gui.exceptionbox( "An Error has occured:\n\n {}".format(c), "HASHIT") else: if not c["hash_check"]: DONE.append("{}: FAILED".format(c["filename"])) else: DONE.append("{}: OK".format(c["filename"])) gui.msgbox('\n'.join(DONE)) elif command == COMMANDS[4]: showhelp() elif command == COMMANDS[5]: exit()