2023-04-24 08:29:10 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
A small script to help analyze the md5 hash function.
|
|
|
|
|
|
|
|
@author Christoph J. Scherr <software@cscherr.de>
|
|
|
|
@license MIT
|
|
|
|
@source: https://git.cscherr.de/PlexSheep/python-dhbw/src/branch/master/src/md5-analyzer.py
|
2023-04-24 17:12:50 +02:00
|
|
|
TODO do I need to implement md5 by myself for the assignment?
|
2023-04-24 08:29:10 +02:00
|
|
|
"""
|
|
|
|
import argparse
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(prog="md5-analyzer", description='md5 analyzer for a cryptography assignment')
|
2023-04-24 17:12:50 +02:00
|
|
|
parser.add_argument('-i', '--input', type=str,
|
2023-04-24 18:48:25 +02:00
|
|
|
help='an input that should be hashed or used with iterate')
|
2023-04-24 17:12:50 +02:00
|
|
|
parser.add_argument('-a', '--print-all', action="store_true",
|
|
|
|
help="print all hashes in iterate mode")
|
|
|
|
parser.add_argument('-t', '--iterate', action="store_true",
|
|
|
|
help='iterate 0 to 999999 (chars), generate hashes for this, analyze it\'s hashes.')
|
2023-04-24 18:48:25 +02:00
|
|
|
parser.add_argument('-q', '--quiet', action="store_true",
|
|
|
|
help="print less")
|
|
|
|
parser.add_argument('-m', '--max', action="store", type=int, default=999999,
|
|
|
|
help="max value for iteration")
|
2023-04-24 08:29:10 +02:00
|
|
|
args = parser.parse_args()
|
2023-04-24 18:48:25 +02:00
|
|
|
|
2023-04-24 17:12:50 +02:00
|
|
|
if args.iterate:
|
|
|
|
if not args.input:
|
|
|
|
args.input = ""
|
2023-04-24 19:10:11 +02:00
|
|
|
if not args.quiet:
|
|
|
|
print('='*80)
|
2023-04-24 17:12:50 +02:00
|
|
|
hashlist = []
|
|
|
|
searchbytes = "0000"
|
2023-04-24 18:48:25 +02:00
|
|
|
for i in range(0, args.max + 1):
|
|
|
|
# args.max should be included, so +x for the counter
|
2023-04-24 17:12:50 +02:00
|
|
|
input = (args.input + str(i))
|
|
|
|
hash = hashlib.md5(input.encode())
|
2023-04-24 19:10:11 +02:00
|
|
|
found = False
|
2023-04-24 17:12:50 +02:00
|
|
|
# print only every 1000 lines for performance
|
|
|
|
if hash.hexdigest()[0:4] == searchbytes:
|
2023-04-24 19:10:11 +02:00
|
|
|
found = True
|
2023-04-24 18:48:25 +02:00
|
|
|
hashlist.append((input, hash.hexdigest()))
|
2023-04-24 19:10:11 +02:00
|
|
|
if not args.quiet and (found or i % 1000 == 0 or args.print_all):
|
|
|
|
print("inp %s\t\t| out %s" % (input, hash.hexdigest()))
|
|
|
|
if found:
|
2023-04-24 18:48:25 +02:00
|
|
|
print("^" * 80)
|
2023-04-24 17:12:50 +02:00
|
|
|
print('='*80)
|
|
|
|
for (index, hash) in hashlist:
|
2023-04-24 18:48:25 +02:00
|
|
|
print("inp %s\t\t| has %s" % (index, hash))
|
2023-04-24 17:12:50 +02:00
|
|
|
print('='*80)
|
2023-04-24 19:10:11 +02:00
|
|
|
size_searchbytes = 16**4
|
|
|
|
expected = args.max / size_searchbytes # 32 hex value chars in an md5 hash
|
|
|
|
print("found %d items (%f%%) from %d" % (len(hashlist), len(hashlist) / size_searchbytes, args.max))
|
|
|
|
print("Expected %f (%f%%) from %d" % (expected, expected / size_searchbytes, args.max))
|
2023-04-24 17:12:50 +02:00
|
|
|
exit()
|
2023-04-24 18:48:25 +02:00
|
|
|
|
2023-04-24 17:12:50 +02:00
|
|
|
if args.input:
|
2023-04-24 08:29:10 +02:00
|
|
|
print("Hash for '%s':\n%s" % (
|
2023-04-24 17:12:50 +02:00
|
|
|
args.input,
|
|
|
|
hashlib.md5(args.input.encode()).digest().hex()
|
2023-04-24 08:29:10 +02:00
|
|
|
))
|
2023-04-24 17:12:50 +02:00
|
|
|
exit()
|
2023-04-24 18:48:25 +02:00
|
|
|
|
|
|
|
# else print help
|
|
|
|
parser.print_help()
|
2023-04-24 08:29:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|