私が自分用に設計したプログラミング演習と、後で非常に安全でないシステムで使用するために、MD5 ハッシュを比較しようとしています。プレーンテキストファイルに保存され、関数によって引き出されるものcheck_pw()
と、CGI フォームから送信されたパスワードから作成されるものです。md5_pw(
) は、プログラム内のすべてのハッシュを作成するために使用されます。
何らかの理由で、if (pair[1] == md5_pw(pw))
always fails、私のプログラムはエラーチェック行に同一のハッシュを出力しますが:
print "this is the pw from the file: ", pair[1], "<br />" print "this is the md5 pw you entered: ", md5_pw(pw), "<br />"
私はどこを台無しにしていますか?
コード:
def md5_pw(pw):
"""Returns the MD5 hex digest of the pw with addition."""
m = md5.new()
m.update("4hJ2Yq7qdHd9sdjFASh9"+pw)
return m.hexdigest()
def check_pw(user, pw, pwfile):
"""Returns True if the username and password match, False otherwise. pwfile is a xxx.txt format."""
f = open(pwfile)
for line in f:
pair = line.split(":")
print "this is the pw from the file: ", pair[1], "<br />"
print "this is the md5 pw you entered: ", md5_pw(pw), "<br />"
if (pair[0] == user):
print "user matched <br />"
if (pair[1] == md5_pw(pw)):
f.close()
return True
else:
f.close()
print "passmatch a failure"
return False