I'm trying to find some passwords using the Rainbow method. I have a CSV file containing people's names and their hashed passwords using SHA-256. I have to retrieve the original passwords which are four-digit numbers [1000-9999].
The CSV file:
danial,99b057c8e3461b97f8d6c461338cf664bc84706b9cc2812daaebf210ea1b9974
elham,85432a9890aa5071733459b423ab2aff9f085f56ddfdb26c8fae0c2a04dce84c
My code:
import hashlib
import csv
def hash_password_hack(passwords, new_passwords):
with open (r'passwords.csv','r', encoding='utf-8') as f:
reader=csv.reader(f)
dict1={}
for row in reader:
dict1[row[1]]=row[0]
dict2={}
for i in range (1000,10000):
hashed_password=hashlib.sha256(str(i).encode('utf-8'))
dict2[hashed_password]=i
for key in dict1:
with open (r'new_passwords.csv', 'w', newline='') as f1:
writer=csv.writer(f1)
password=dict2[key]
name=dict1[key]
writer.writerow([name,password])
When I run the code, following error appears:
KeyError: '99b057c8e3461b97f8d6c461338cf664bc84706b9cc2812daaebf210ea1b9974'
As I know, this error appears when the dictionary I try to call doesn't have that specific key. Since I've hashed every number between 1000 and 9999, dict2
has to contain the above key. Why do I get this error, and how can I solve it?
hashlib.sha256(str(i).encode('utf-8'))
returns a HASH
object, not the hex hash, so you are keying by object. Instead, you want to use the hexdigest()
method to get the hex hash e.g. hashed_password = hashlib.sha256(str(i).encode('utf-8')).hexdigest()
.