md5 analy update

This commit is contained in:
Christoph J. Scherr 2023-04-24 18:48:25 +02:00
parent 53c6c6eb94
commit 5e38f6cdf5
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
1 changed files with 21 additions and 12 deletions

View File

@ -13,44 +13,53 @@ import hashlib
def main(): def main():
parser = argparse.ArgumentParser(prog="md5-analyzer", description='md5 analyzer for a cryptography assignment') parser = argparse.ArgumentParser(prog="md5-analyzer", description='md5 analyzer for a cryptography assignment')
parser.add_argument('-i', '--input', type=str, parser.add_argument('-i', '--input', type=str,
help='an input that should be hashed.') help='an input that should be hashed or used with iterate')
parser.add_argument('-a', '--print-all', action="store_true", parser.add_argument('-a', '--print-all', action="store_true",
help="print all hashes in iterate mode") help="print all hashes in iterate mode")
parser.add_argument('-t', '--iterate', action="store_true", parser.add_argument('-t', '--iterate', action="store_true",
help='iterate 0 to 999999 (chars), generate hashes for this, analyze it\'s hashes.') help='iterate 0 to 999999 (chars), generate hashes for this, analyze it\'s hashes.')
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")
args = parser.parse_args() args = parser.parse_args()
if args.iterate: if args.iterate:
if not args.input: if not args.input:
args.input = "" args.input = ""
print('='*80) print('='*80)
hashlist = [] hashlist = []
max = 999999
searchbytes = "0000" searchbytes = "0000"
for i in range(0, max + 1): for i in range(0, args.max + 1):
# max should be included, so +x for the counter # args.max should be included, so +x for the counter
input = (args.input + str(i)) input = (args.input + str(i))
hash = hashlib.md5(input.encode()) hash = hashlib.md5(input.encode())
# print only every 1000 lines for performance # print only every 1000 lines for performance
if i % 1000 == 0 or args.print_all: if not args.quiet and i % 1000 == 0 or args.print_all:
print("inp %s\t\t| out %s" % (input, hash.hexdigest())) print("inp %s\t\t| out %s" % (input, hash.hexdigest()))
if hash.hexdigest()[0:4] == searchbytes: if hash.hexdigest()[0:4] == searchbytes:
hashlist.append((i, hash.hexdigest())) hashlist.append((input, hash.hexdigest()))
if not args.quiet:
print("^" * 80) print("^" * 80)
print('='*80) print('='*80)
for (index, hash) in hashlist: for (index, hash) in hashlist:
print("ind %i\t\t| has %s" % (index, hash)) print("inp %s\t\t| has %s" % (index, hash))
print('='*80) print('='*80)
size_of_first_x = 2**16 size_searchbytes = 2**16
expected = size_of_first_x / max expected = 16**32 / size_searchbytes # 32 hex value chars in an md5 hash
print("found %d items.\nExpected %d (%f%%) from %d" % (len(hashlist), expected, expected / size_of_first_x, max)) print("found %d items (%f%%)" % (len(hashlist), len(hashlist) / size_searchbytes))
print("Expected %d (%f%%) from %d" % (expected, expected / size_searchbytes, args.max))
exit() exit()
if args.input: if args.input:
print("Hash for '%s':\n%s" % ( print("Hash for '%s':\n%s" % (
args.input, args.input,
hashlib.md5(args.input.encode()).digest().hex() hashlib.md5(args.input.encode()).digest().hex()
)) ))
exit() exit()
parser.print_usage()
# else print help
parser.print_help()