Python program which could auto sort and replace hashes with plain password in csv file

Viewed 26

I have an excel file "ex.csv" with columns - Hash, Salt, Name And I have a txt file "found.txt" where are decrypted hashes. Their format is Hash: Salt: Plain_Password I would like to change Hash from "ex.csv" with Plain_Password from "found.txt". Would like to know, how I could do that :) I have written a test program that would output into separate txt file the Hash: Salt but it is not working.

Python code -

# File Reads
a = open("ex.csv")
b = open("found.txt")

# Reading contents

ex = a.read()
found = b.read()

# Splitting files by newline

ex_s = ex.split("\n")
found_s = found.split("\n")

# Splitting them into subarrays by splitting them by ','

temp_exsp2 = []
temp_foundsp2 = []

i=0
for item in ex_s:
    temp_exsp2[i] = item[0] # Presumeably here's an error
    i+=1

i=0

for item in found_s:
    temp_foundsp2[i] = item[0] # Same thing here
    i+=1


i=0
z=0 #Used for incrementing found array

FoundArray0 = [] #For line from ex
FoundArray1 = [] #For line from found

while i!=len(ex_s): # Main comparison loop
    for item in temp_foundsp2: # Inner loop for looping through all found file
        j=0
        if item in temp_exsp2[i]:
            FoundArray0[z] = i
            FoundArray1[z] = j
            z+=1
        j+=1
    i+=1 # Go to the next line in the ex.csv


output = open("output.txt","w")

for out in FoundArray0:
    for out2 in FoundArray1:
        output.write(str(ex_s[FoundArray0]) + ":" + str(temp_foundsp2[FoundArray1]))

FoundArray here is the line numbers from ex.csv and found.txt (Would like to know if there's a way to do it better ;) Because I feel that it is not right) It is giving me an error - temp_exsp2[i] = item[0] # Presumably here's an error IndexError: list assignment index out of range

Samples from ex.csv:

210ac64b3c5a570e177b26bb8d1e3e93f72081fd,gx0FMxymN,user1
039e8c304c9ada05fd9cc549ac62e178edbfaed6,eVRCBE2OG,user2

Samples from found.txt

f8fa3b3da3fc71e1eaf6c18e4afef626e1fc7fc1:t7e2jlLvs:pass1
bce61cb17c381e11afbcf89ab30ae5cc8276722f:rjCAX5D6K:pass2

Maybe there's an excel function that does that :D I don't know. I am new at python and would like to know the best way to realize this :) Thanks ;)

1 Answers

To split string that has entries separated by a specific delimeter you can use string.split(delimeter) method.

Example:

a = '123:456:abc'
a.split(':')
>>> ['123', '456', 'abc'] 

You could also take a look at pandas DataFrame which is able to load csv file and then lets you easily manipulate the columns and much more.

Related