[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

md5

hi i want to get a password input from the user and then it is stored as md5 hash in a file. The md5 is then check whether it matches the password entered by the user.

dict = {}
if(dict.has_key(user)):

      f = open("test", "w")
      line2 = f.readline()[:-1]

      while f:
         dict[line[1]] = md5.md5(passWord)
         f.write(dict[line2[1]].digest())
         line = f.readline()[:-1]

      if (dict[line2[1]] == pwd):
         do something

i am not sure the above is correct. any help is of use thanks.
0
darrenteo82
Asked:
darrenteo82
  • 2
  • 2
2 Solutions
 
ramromconsultant Commented:
Why are you not sure? Have you tested the program and gotten unexpected results? We can help you better when we know where you are stuck. I hope that this code is part of a program, since it will not get past line 2 as it is. Please show us a complete program and the contents of the "test" file.

There are numerous problems which suggest a need for some desk testing*, then running the program.

*desk testing = with pencil and paper "execute" each line of code in execution order and write down what happens. Keep a list of variable names and their values as they are [re]assigned.

For example:
line 1 dict == {}
line 2 user == oops user has not been defined.
...
line 8 line == oops line has not been defined.

Bob Gailer
Page Editor
0
 
confusedmoronCommented:
Looks like you want something like this?
import md5,string
#create fake file
fakeFile="username,"+md5.md5("password").digest()+"\n"
fakeFile+="joe,"+md5.md5("bloggs").digest()+"\n"
fakeFile+="bob,"+md5.md5("monkey").digest()+"\n"
f=open("users.txt","w")
f.write(fakeFile)
f.close()
#load info from file into dictionary
unap={}
f=open("users.txt","r")
for lines in f.readlines():
    splitStr=string.split(lines,",")
    unap[splitStr[0]]=string.strip(splitStr[1])
f.close()
#get username and password
userName=raw_input("Username:")
passWord=raw_input("Password:")
#match username and password to dictionary unap
if(unap.has_key(userName)):
    if(unap[userName]==md5.md5(passWord).digest()):
        print "Username and password match"
    else:
        print "Password wrong"
else:
    print "No such user"
0
 
ramromconsultant Commented:
confusedmoron: I am disappointed that darrenteo8 has not responded. I has hoped to nudge him toward self-dependence in writing and debugging code. I'm not sure giving him a solution is the best approach, but who knows.

darrenteo8 - is any of the above helpful? We'd like to hear from you!

Bob Gailer
Page Editor
0
 
confusedmoronCommented:
I've always liked to have a working example of things I didn't understand. That why I could look at it and fully understand what was going on. Otherwise I would find myself throwing bits of code in until it works and never fully understanding why it works.
Personal preference I guess.
Would love to hear from darrenteo8!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now