Reputation: 107
I'm trying to have python read some lines of text from a file and then convert them to an md5 hash to compare to the one the user entered.
I'm using f = open(file, 'r')
to open and read the files and everything is working fine but when it hashes the word its not the right hash for that word.
So I need to know how to remove the spaces or the \n
at the end that is causing it to hash incorrectly.
If that makes sense. I didnt really know how to word it.
The code: http://pastebin.com/Rdticrbs
Upvotes: 2
Views: 18097
Reputation: 29302
You can just open the file like this:
with open('file', 'r') as f:
for line in f:
do_somthing_with(line.strip())
From the official documentation strip()
will return a copy of the string with the leading and trailing characters removed.
Edit: I correct my mistake thanks to the comment of katrielalex
(I don't know why I believed what I posted before). My apology.
Upvotes: 1
Reputation: 3185
Return a copy of the string with the leading and trailing characters removed.The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
>>> s = " Hello \n".strip() >>> print(s) ... Hello
In your code, add this.
words = lines[num].strip()
Upvotes: 0
Reputation: 123742
I have just rewritten your pastebin code, because it's not good. Why did you write it recursively? (The line sys.setrecursionlimit(10000000)
should probably be a clue that you're doing something wrong!)
import md5
hashed = raw_input("Hash:")
with open(raw_input("Wordlist Path: ")) as f:
for line in f:
if md5.new(line.strip()).hexdigest() == hashed:
print(line.strip())
break
else:
print("The hash was not found. Please try a new wordlist.")
raw_input("Press ENTER to close.")
This will obviously be slow, because it hashes every word in the wordlist every time. If you are going to look up more than one word in the wordlist, you should compute (once) a mapping of hashes to words (a reverse lookup table) and use that. You may need a large-scale key-value storage library if your wordlists are large.
Upvotes: 3
Reputation: 91132
def readStripped(path):
with open('file') as f:
for line in f:
yield f.strip()
dict((line, yourHash(line)) for line in readStripped(path))
Upvotes: 0