Skip to content

Instantly share code, notes, and snippets.

@rickithadi
Created September 14, 2024 14:31
Show Gist options
  • Save rickithadi/07c5502e1f2cacdbb4079ea28cd65d20 to your computer and use it in GitHub Desktop.
Save rickithadi/07c5502e1f2cacdbb4079ea28cd65d20 to your computer and use it in GitHub Desktop.
import hashlib
import random
import time
# The target hash to compare against
TARGET_HASH = "ee2b34b775d70cd1e23ff42a7ed39731419a443c4f08b062e96015e6e951588d"
fake_file= 'confession_fake.txt'
forged_file= 'confession_forged.txt'
hashDigits=5
def getEndOfHash(string,length):
return hashlib.sha256(string.encode()).hexdigest()[0-length:]
def convertArrayToString(lineArray):
string=""
for l in lineArray:
string+=l
string+= "\n"
return string
fakeMessage=open(fake_file,"r").read()
forgedMessageArray=fakeMessage.split('\n')[:-1]
NumberOfLines=len(forgedMessageArray)
targetString = TARGET_HASH[-hashDigits:]
print('looking for', targetString)
lastHash=""
attempt=1
start=time.time()
while lastHash!=targetString:
forgedMessageArray[attempt%NumberOfLines]+= " "
forgedMessage= convertArrayToString(forgedMessageArray)
lastHash=getEndOfHash(forgedMessage,hashDigits)
print(lastHash)
attempt+=1
totalTime =time.time()-start
print("%d attempts, %f seconds" %(attempt, totalTime))
open(str(hashDigits)+ "-"+ forged_file, 'w',newline='\n').write(forgedMessage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment