From 0cf94c6a60d6939404e2c83241ad89382238aab2 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Tue, 10 Jan 2023 19:26:56 +0200 Subject: [PATCH] Add Lesson 8 Task e1900ba2 --- Lesson_8/We know/sha256_crack.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Lesson_8/We know/sha256_crack.py diff --git a/Lesson_8/We know/sha256_crack.py b/Lesson_8/We know/sha256_crack.py new file mode 100644 index 0000000..f49c113 --- /dev/null +++ b/Lesson_8/We know/sha256_crack.py @@ -0,0 +1,35 @@ +# Lesson 8 Task e1900ba2: SHA-256 crack (?) +# Author: Stanislav Mykhailenko +# License: Unlicense + +# Return codes: +# 0 - OK +# 1 - Invalid hash +# 2 - Not cracked + +import hashlib, itertools, sys + +def hashError(): + print("Invalid SHA-256 hash.") + sys.exit(1) + +crackedHash = input("Enter SHA-256 hash: ") + +if len(crackedHash) != 64: + hashError() + +try: + int(crackedHash, 16) +except ValueError: + hashError() + +for product in itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=4): + string = '' + for i in range(len(product)): + string += product[i] + if crackedHash == hashlib.sha256(string.encode('utf-8')).hexdigest(): + print("Cracked: " + string) + sys.exit(0) + +print("Not cracked.") +sys.exit(2)