2

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

user1152873
  • 107
  • 1
  • 2
  • 9
  • 2
    " I didnt really know how to word it.". When in doubt show (1) code, (2) input, (3) actual output and (4) expected output. – S.Lott Jan 16 '12 at 23:56
  • Please **update** the question with code, input, actual output, and expected output. Please post the **smallest** piece of code that shows the problem. – S.Lott Jan 17 '12 at 03:36

4 Answers4

3

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.

Community
  • 1
  • 1
Katriel
  • 120,462
  • 19
  • 136
  • 170
  • I got with f as open(raw_input("Wordlist Path: ")): SyntaxError: can't assign to function call as an error with that. – user1152873 Jan 17 '12 at 00:15
  • Sorry, wasn't thinking when I typed. You should look into the `with` statement as you haven't seen it before: it's great! (Fixed.) – Katriel Jan 17 '12 at 00:22
1

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.

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
  • 2
    No, it won't. `for line in f.readlines()` has precisely the same outcome as `for line in f` except that it reads the entire file into memory first, which is wasteful. Also, don't shadow the builtin class `file`. – Katriel Jan 16 '12 at 23:56
  • @katrielalex: My apology, you sure are right I don't know why I believed that. I usually write in `python-3` so I don't have the problem of having a builtin class `file`, but I changed that to `f` anyway for `python-2` compatibility. – Rik Poggi Jan 17 '12 at 00:15
  • @katrielalex: I think it's better with `readlines()`, this way the code could be run with either `python-2` and `python-3`. – Rik Poggi Jan 17 '12 at 00:23
  • Python 2 has supported iterating over a file object since version 2.3 (http://docs.python.org/library/stdtypes.html#file.next). Did you try it? You might be surprised as to how many Python 3 features were backported to Python 2! – Katriel Jan 17 '12 at 00:24
0

str.strip([chars])

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()
JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
0
def readStripped(path):
    with open('file') as f:
        for line in f:
            yield f.strip()

dict((line, yourHash(line)) for line in readStripped(path))
ninjagecko
  • 88,546
  • 24
  • 137
  • 145