473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Comparing values in loops

Thekid
145 100+
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
9 2521
bvdet
2,851 Expert Mod 2GB
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
145 100+
:) 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
2,851 Expert Mod 2GB
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
145 100+
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
2,851 Expert Mod 2GB
Oops! I meant:
Expand|Select|Wrap|Line Numbers
  1. for i, item in enumerate(hashed_words):
Sep 25 '09 #6
Thekid
145 100+
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
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. for word in words:
  2.     m=hashlib.md5(word.strip())
Sep 25 '09 #8
Thekid
145 100+
That works the way I need it! Thank you.
Sep 25 '09 #9
Glenton
391 Expert 256MB
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

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

Similar topics

4
by: Jorgen Gustafsson | last post by:
Hi, im trying to write a small progam to compare data in 2 textfiles. I want to search for values that doesnt exist in File2. The result should be "3" in the example below but Im not able to do...
3
by: twdo | last post by:
Let me see if I can explain my situation clearly. I have a table with the columns: answer_id, question_id, member_id, answer - answer_id is the primary key for the table. - question_id...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
2
by: darrel | last post by:
I have two comma delimted strings that I need to compare individual values between the two. I assume the solution is likely to put them into an array? If so, do I need to loop through one,...
2
by: John McMonagle | last post by:
Say I have a dictionary like below: d = {(100,500):, (100,501):, (100,502):} Say I want to multiply all the values of the dictionary by 2: for key in d.keys(): d = map(lambda x: x*2,...
5
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.