443,694 Members | 1,846 Online
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

 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 import hashlib,string # the given hash, which is actually "12345" x="827ccb0eea8a706c4c34a16891f84e7b" f=open("words.txt","r") words = f.readlines() # my loop to run through the words, converting to md5 for word in words:      m=hashlib.md5(word)      k=m.hexdigest()      print k   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 import hashlib,string # the given hash, which is actually "12345" x="827ccb0eea8a706c4c34a16891f84e7b" f=open("words.txt","r") words = f.readlines() # my loop to run through the words, converting to md5 for word in words:      m=hashlib.md5(word)      k=m.hexdigest()      # this part only works if the value of 'x' is the very-       # last word in the list:     if k==x:     print "Yes!"   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 import hashlib,string # the given hash, which is actually "12345" x="827ccb0eea8a706c4c34a16891f84e7b" f=open("words.txt","r") words = f.readlines() # my loop to run through the words, converting to md5 for word in words:      m=hashlib.md5(word)      k=m.hexdigest()      infile=open("data.txt","w")      out.write(str(k))      out.close()      infile=open("data.txt","r")      lines=infile.readlines()      for line in lines:          if line == x:          print "Item found!"   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
9 Replies

 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

 100+ P: 145 :) Thanks, didn't think about that! This works: Expand|Select|Wrap|Line Numbers import hashlib,string # the given hash, which is actually "12345" x="827ccb0eea8a706c4c34a16891f84e7b" f=open("words.txt","r") words = f.readlines() # my loop to run through the words, converting to md5 for word in words:      m=hashlib.md5(word)      k=m.hexdigest()      numbs=[k]      for num in numbs:           if num==x:              print "Yes!"   Sep 25 '09 #3

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

 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

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

 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

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

 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 # for word in words: #      m=hashlib.md5(word) #      k=m.hexdigest() #      # this part only works if the value of 'x' is the very-  #      # last word in the list: #     if k==x: #     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