Comparing values in loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    Comparing values in loops

    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:

    Code:
    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'.

    Code:
    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:

    Code:
    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'?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Why not save the hashed words in a list? Then execute a separate for loop on the list, comparing each hash to "x".

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      :) Thanks, didn't think about that! This works:

      Code:
      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!"

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I was thinking of something like this:
        Code:
        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)

        Comment

        • Thekid
          New Member
          • Feb 2007
          • 145

          #5
          When I try that method I get this error:
          for i, item in hashed_words:
          ValueError: too many values to unpack

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Oops! I meant:
            Code:
            for i, item in enumerate(hashed_words):

            Comment

            • Thekid
              New Member
              • Feb 2007
              • 145

              #7
              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 = 827ccb0eea8a706 c4c34a16891f84e 7b
              apple = 1f3870be274f6c4 9b3e31a0c672895 7f
              boris = 4dbf44c6b1be736 ee92ef90090452f c2
              dorothy = c5483d8bfb22e65 a48099ac0075ed6 4b

              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?

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Code:
                for word in words:
                    m=hashlib.md5(word.strip())

                Comment

                • Thekid
                  New Member
                  • Feb 2007
                  • 145

                  #9
                  That works the way I need it! Thank you.

                  Comment

                  • Glenton
                    Recognized Expert Contributor
                    • Nov 2008
                    • 391

                    #10
                    Hi

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

                    Code:
                    # 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!?

                    Comment

                    Working...