Connecting Tech Pros Worldwide Help | Site Map

Comparing values in loops

Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#1: Sep 25 '09
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'?
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#2: Sep 25 '09

re: Comparing values in loops


Why not save the hashed words in a list? Then execute a separate for loop on the list, comparing each hash to "x".
Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#3: Sep 25 '09

re: Comparing values in loops


:) 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.  
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#4: Sep 25 '09

re: Comparing values in loops


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)
Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#5: Sep 25 '09

re: Comparing values in loops


When I try that method I get this error:
for i, item in hashed_words:
ValueError: too many values to unpack
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#6: Sep 25 '09

re: Comparing values in loops


Oops! I meant:
Expand|Select|Wrap|Line Numbers
  1. for i, item in enumerate(hashed_words):
Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#7: Sep 25 '09

re: Comparing values in loops


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?
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#8: Sep 25 '09

re: Comparing values in loops


Expand|Select|Wrap|Line Numbers
  1. for word in words:
  2.     m=hashlib.md5(word.strip())
Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#9: Sep 25 '09

re: Comparing values in loops


That works the way I need it! Thank you.
Member
 
Join Date: Nov 2008
Posts: 47
#10: Sep 29 '09

re: Comparing values in loops


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!?
Reply