Lesson 4 of 6
The hash
- 20 min
- 4 checks
- 3 hints
- Go to task
Goal
Brute-force search over a small space
Explanation
Some programs don't store the secret code itself. They store its hash instead, then hash whatever you type and compare the two hashes. A hash only works in one direction, so you can't compute the code from it directly.
But if the code is short and you know which characters it can use, there aren't many possibilities, so you can hash each one until you find the match. This is called brute force. The hash here is SHA-256, and you compute it like this:
hashlib.sha256(text.encode()).hexdigest()
A short code falls to brute force, as you're about to see. That's why real systems protect passwords with long secrets, add a random salt to each password, and use hashes that are slow on purpose, like bcrypt, scrypt and Argon2, so that brute force becomes very expensive.
Your task
Write a function crack(target_hex, length, alphabet) that returns the code that is length characters long, uses characters from the string alphabet, and has the hash target_hex. If there's no such code, return None. TARGET holds the hash of a four-digit code for you to test on, and the checks give your function other hashes.
Checks
4 checks- Recovers a four-digit code
- Recovers a code made of letters
- Works on a five-character code
- Returns None when no code matches