By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,694 Members | 1,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,694 IT Pros & Developers. It's quick & easy.

Comparing values in loops

Thekid
100+
P: 145
Hi, I can't seem to figure this out. Here's my objective: I have a value that is an md5 hash and I have a wordlist. I need to md5 the words in the list, then compare them to the given hash, then have the actual word print out. So I figured out how to take the list and md5 the words and get them to print out. Now I need to compare them but when I do this, it's only saving the very last word that was hashed. Here are some things I've tried:

Expand|Select|Wrap|Line Numbers
  1. import hashlib,string
  2. # the given hash, which is actually "12345"
  3. x="827ccb0eea8a706c4c34a16891f84e7b"
  4. f=open("words.txt","r")
  5. words = f.readlines()
  6. # my loop to run through the words, converting to md5
  7. for word in words:
  8.      m=hashlib.md5(word)
  9.      k=m.hexdigest()
  10.      print k
  11.  
That will successfully print out all of the converted values in the wordlist but now I need to compare it to 'x'.

Expand|Select|Wrap|Line Numbers
  1. import hashlib,string
  2. # the given hash, which is actually "12345"
  3. x="827ccb0eea8a706c4c34a16891f84e7b"
  4. f=open("words.txt","r")
  5. words = f.readlines()
  6. # my loop to run through the words, converting to md5
  7. for word in words:
  8.      m=hashlib.md5(word)
  9.      k=m.hexdigest()
  10.      # this part only works if the value of 'x' is the very- 
  11.      # last word in the list:
  12.     if k==x:
  13.     print "Yes!"
  14.  
Since it's only using the very last value of 'k' I thought I'd try to save all of the converted values to a text file, then open that up and use another 'for' statement:

Expand|Select|Wrap|Line Numbers
  1. import hashlib,string
  2. # the given hash, which is actually "12345"
  3. x="827ccb0eea8a706c4c34a16891f84e7b"
  4. f=open("words.txt","r")
  5. words = f.readlines()
  6. # my loop to run through the words, converting to md5
  7. for word in words:
  8.      m=hashlib.md5(word)
  9.      k=m.hexdigest()
  10.      infile=open("data.txt","w")
  11.      out.write(str(k))
  12.      out.close()
  13.      infile=open("data.txt","r")
  14.      lines=infile.readlines()
  15.      for line in lines:
  16.          if line == x:
  17.          print "Item found!"
  18.  
I don't really think it's necessary for me to have to save the values first but even when I try that route it only saves and writes the very last 'k' value, not all of them. So my question is:
How can I get the value of 'x' to compare to every value of 'k'?
Sep 25 '09 #1
Share this Question
Share on Google+
9 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Why not save the hashed words in a list? Then execute a separate for loop on the list, comparing each hash to "x".
Sep 25 '09 #2

Thekid
100+
P: 145
:) Thanks, didn't think about that! This works:

Expand|Select|Wrap|Line Numbers
  1. import hashlib,string
  2. # the given hash, which is actually "12345"
  3. x="827ccb0eea8a706c4c34a16891f84e7b"
  4. f=open("words.txt","r")
  5. words = f.readlines()
  6. # my loop to run through the words, converting to md5
  7. for word in words:
  8.      m=hashlib.md5(word)
  9.      k=m.hexdigest()
  10.      numbs=[k]
  11.      for num in numbs:
  12.           if num==x:
  13.              print "Yes!"
  14.  
Sep 25 '09 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I was thinking of something like this:
Expand|Select|Wrap|Line Numbers
  1. x="827ccb0eea8a706c4c34a16891f84e7b"
  2. hashed_words = []
  3. for word in words:
  4.     m=hashlib.md5(word)
  5.     hashed_words.append(m.hexdigest())
  6.  
  7. for i, item in hashed_words:
  8.     if item == x:
  9.         print "Line number %s matches" % (i+1)
Sep 25 '09 #4

Thekid
100+
P: 145
When I try that method I get this error:
for i, item in hashed_words:
ValueError: too many values to unpack
Sep 25 '09 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Oops! I meant:
Expand|Select|Wrap|Line Numbers
  1. for i, item in enumerate(hashed_words):
Sep 25 '09 #6

Thekid
100+
P: 145
After some more testing I've discovered another problem. It seems that the hashes aren't correct because the newline char is being added to the words in the list. Trying .replace or .split gives an error message there's no attribute of those for a list. Here is a wordlist with there actual md5 hashes:

12345 = 827ccb0eea8a706c4c34a16891f84e7b
apple = 1f3870be274f6c49b3e31a0c6728957f
boris = 4dbf44c6b1be736ee92ef90090452fc2
dorothy = c5483d8bfb22e65a48099ac0075ed64b

If I add a 'print words' line in the code I get:

['12345\n', 'apple\n', 'boris\n', 'dorothy']

So the last word prints out the actual hash because it doesn't have a '\n' but the rest are including it in the hash. How can I remove that char from a list?
Sep 25 '09 #7

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. for word in words:
  2.     m=hashlib.md5(word.strip())
Sep 25 '09 #8

Thekid
100+
P: 145
That works the way I need it! Thank you.
Sep 25 '09 #9

Expert 100+
P: 391
Hi

I noticed that in your original post, your second bit of code had the following:

Expand|Select|Wrap|Line Numbers
  1. # for word in words:
  2. #      m=hashlib.md5(word)
  3. #      k=m.hexdigest()
  4. #      # this part only works if the value of 'x' is the very- 
  5. #      # last word in the list:
  6. #     if k==x:
  7. #     print "Yes!"
These last two lines are not indented to the same level. Could this be the reason it only compared with the last value!?
Sep 29 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.