Python Forum
how can I generate a password in hashlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can I generate a password in hashlib
#11
@DeaD_EyE - if you check the other threads of @go127a you will see they are basic homeworks assignments. So I think it's more reading/writing from files, working wit data structures etc. with pinch of "hacker thrill" in this case. By the way I find this site very helpful and informative as introduction to the topis of password handling - https://crackstation.net/hashing-security.htm

@go127a - you already know how to write to a file as we see in your other thread - https://python-forum.io/showthread.php?tid=18147
you need
and your approach is not correct.

you need to open a file for reading and a file for writing (2 files)
read line by line from the input file.
using the hash from the file and the rainbow dict - find the password
write the name (from input file) and the password (from the dict) to output file
continue with the next line

I stop here because I already give away too much for a homework
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
I know how can I write the csv file, but I dont know how can I fix the output in order to reach the prefer output.
here is the output of my code.
Quote:d,a,n,i,a,l
5,1,0,4
p,e,t,e,r
9,7,7,0

and its false.
Reply
#13
I have a list like:
Quote:['danial', '5104', 'elham', '9770']

I should write every name and password like below format on output csv file:
Quote:daniel,5104
elham,9770

I have used below code, but I dont know where I should update my list to make a correct output.
the output of my code is like below Wall :
Quote:d,a,n,i,a,l,",",5,1,0,4
e,l,h,a,m,",",9,7,7,0

the code which I have used:
esm=data[::2]
#print(esm)
pas=data[1::2]
#print(pas)
#for i in range(0,len(esm)):
    #dataf=[]
    #print('{},{}'.format(esm[i], pas[i]))
    
    #dataf.append(('{},{}'.format(esm[i], pas[i])))
    
with open('code.csv', 'w', newline='') as output_file_name:
        writer = csv.writer(output_file_name)
        for i in range(0,len(esm)):
            dataf=[]
            #print('{},{}'.format(esm[i], pas[i]))
            dataf.append(('{},{}'.format(esm[i], pas[i])))
            writer.writerows(dataf)
        output_file_name.close()
Reply
#14
@go127a, please read carefully and follow what you are told to do

(May-07-2019, 06:22 PM)buran Wrote: you need to open a file for reading and a file for writing (2 files)
read line by line from the input file.
using the hash from the file and the rainbow dict - find the password
write the name (from input file) and the password (from the dict) to output file
continue with the next line

if you want to first process all of the input file and only then to write to file, then you need to come up with a structure like this
[['danial', '5104'], ['elham', '9770']], i.e. list of lists and then pass this data structure to writer.writerows()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#15
yes I have tried to make the question shorter,

anyway here is the final code which I have a problem to write it out in that order which I have said:
#csvfilecontain:
   #danial,99b057c8e3461b97f8d6c461338cf664bc84706b9cc2812daaebf210ea1b9974
   #elham,85432a9890aa5071733459b423ab2aff9f085f56ddfdb26c8fae0c2a04dce84c
import csv
import itertools
from collections import OrderedDict
import hashlib

with open ('code.csv', 'r') as input_file_name:
    reader=csv.reader(input_file_name)
    key=[]
    val1=[]
    for row in reader:
        name = row[0]
        val=[str(num) for num in row[1:]]
        key.append(name)
        val1.append(val)
        value = list(itertools.chain.from_iterable(val1)) #making a simple list from list of list in value
        value=[str(i) for i in value]
    #print(key)
    dict_csv = dict(zip(key, value))  #making a dictionary from the csv file
#print(dict_csv)
listval=[]
for item in dict_csv.values():
    #print(item)
    listval.append(item) 
#print(listval)

rainbow_dict = {}
for i in range(1000, 10000): # note you need upper end to be 10000 in odrder to include 9999
    my_hash = hashlib.sha256(str(i).encode()).hexdigest()
    rainbow_dict[my_hash] = i
#print(rainbow_dict)
list1=[k for k,v in dict_csv.items() if v in listval]
#print(list1)
list2=[v for k,v in rainbow_dict.items() if k in listval]
#print(list2)
####
list3 = [ item for pair in zip(list1, list2 + [0]) for item in pair]
#print(list3)
data=[]
for item in list3:
    #data=[]
    l3=[]
    l3.append(item)
    print(l3)
    data.append(str(item))
print(data)
esm=data[::2]
#print(esm)
pas=data[1::2]

    
with open('code.csv', 'w', newline='') as output_file_name:
        writer = csv.writer(output_file_name)
        for i in range(0,len(esm)):
            dataf=[]
            #print('{},{}'.format(esm[i], pas[i]))
            dataf.append(('{},{}'.format(esm[i], pas[i])))
            writer.writerows(dataf)
        output_file_name.close()
Reply
#16
(May-14-2019, 12:39 PM)buran Wrote: @go127a, please read carefully and follow what you are told to do
(May-07-2019, 06:22 PM)buran Wrote: you need to open a file for reading and a file for writing (2 files) read line by line from the input file. using the hash from the file and the rainbow dict - find the password write the name (from input file) and the password (from the dict) to output file continue with the next line
if you want to first process all of the input file and only then to write to file, then you need to come up with a structure like this [['danial', '5104'], ['elham', '9770']], i.e. list of lists and then pass this data structure to writer.writerows()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
thanks I have made a new list from data on the previous code.
and I can reach the perfect results. except an extra empty line at the end. which I dont know how to remove it.
Quote:data= ['Daniel', '5104', 'elham', '9770']

    dataf=([data[i:i+2] for i in range(0, len(data), 2)])
and here is my output on the csv file. I have used below code to write on the csv file.
    with open(output_file_name, 'w', newline='') as output_handler:
        writer = csv.writer(output_handler)
        writer.writerows(dataf)
        output_handler.close()
and the output is:

Quote:danial,5104
elham,9770
####I receive an empty line here !!!
Reply
#18
when using with context manager, you don't need to call close(), i.e. you don't need output_handler.close()

And please, stop this nonsense about the extra empty line. You must understand that every line end s with new line character. So what you see as elham,9770 is in fact elham,9770\n. That's the correct thing to have. As a result there is what you see as empty line. THAT IS NORMAL. In your other thread https://python-forum.io/Thread-Avoiding-...s-csv-file perfringo asked you some question trying to explain this to you. Think what will happen if you don't have new line char at the end of last line with data and you or some one else want to append new data. You will append on the last line, not as a new line. Line ALWAYS end with newline character
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#19
And here is what you really need to do
import csv
import hashlib

# create rainbow table
rainbow_table = {}
for i in range(1000, 10000): # note you need upper end to be 10000 in odrder to include 9999
    my_hash = hashlib.sha256(str(i).encode()).hexdigest()
    rainbow_table[my_hash] = i

# read the input file
with open ('code.csv', 'r') as in_file:
    rdr=csv.reader(in_file)
    data = [[name, rainbow_table.get(my_hash, '')] for name, my_hash in rdr]

# print data to see what you've got
print(data)

with open('decoded.csv', 'w', newline='') as out_file:
        wrtr = csv.writer(out_file)
        wrtr.writerows(data)
if you don't want to process the entire file in memory before write to output file eventually you may
import csv
import hashlib

# create rainbow table
rainbow_table = {}
for i in range(1000, 10000): # note you need upper end to be 10000 in odrder to include 9999
    my_hash = hashlib.sha256(str(i).encode()).hexdigest()
    rainbow_table[my_hash] = i

# read the input file, write to output
with open ('code.csv', 'r') as in_file, open('decoded.csv', 'w', newline='') as out_file:
    rdr=csv.reader(in_file)
    wrtr = csv.writer(out_file)
    for name, my_hash in rdr:
        data = [name, rainbow_table.get(my_hash, '')]
        wrtr.writerow(data) 
likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#20
it works, I wonder that you wrote it in 16 lines!!


There is problem, when some new name came into the game. for example if i have input like below:
Quote:danial,99b057c8e3461b97f8d6c461338cf664bc84706b9cc2812daaebf210ea1b9974
elham,85432a9890aa5071733459b423ab2aff9f085f56ddfdb26c8fae0c2a04dce84c
stefi,85432a9890aa5071733459b423ab2aff9f085f56ddfdb26c8fae0c2a04dce84c

for the third line in input, the code will return '0' which is incorrect. (the reason for that is: the second and the third are the similar pass) but I dont know how to save it.

also I should use (OrderedDict from collections library) to make a dictionary and solve it with dictionary.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] How to crack hash with hashlib Milan 0 1,415 Mar-09-2023, 08:25 PM
Last Post: Milan
  Python3 hashlib ogautier 1 1,540 Mar-28-2022, 04:42 AM
Last Post: snippsat
  Confusion about Hashlib Vysero 2 2,989 Jun-25-2018, 04:05 PM
Last Post: DeaD_EyE
  Using SHA3 (keccak) from Hashlib CryptoFlo 0 7,713 Mar-14-2018, 10:56 AM
Last Post: CryptoFlo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020