0

私が自分用に設計したプログラミング演習と、後で非常に安全でないシステムで使用するために、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
4

2 に答える 2

2

おそらくpair[1]末尾に改行があります。試す:

for line in f:
    line = line.rstrip()
    pair = line.split(":")
    # ...etc
于 2009-07-23T05:19:10.850 に答える
1

おそらく改行文字が原因で、ファイルの読み込み/解析に問題があると思います。コードを切り詰めることで、ロジックが適切であることがわかりました。

def md5_pw(pw):
    m = md5.new()
    m.update("4hJ2Yq7qdHd9sdjFASh9"+pw)
    return m.hexdigest()

def check_pw(pw):
    pair = ("c317db7d54073ef5d345d6dd8b2c51e6")
    if (pair == md5_pw(pw)):
        return True
    else:
        return False

>>> import md5
>>> check_pw('fakepw')
False
>>> check_pw('testpw')
True

(「c317db7d54073ef5d345d6dd8b2c51e6」は「4hJ2Yq7qdHd9sdjFASh9testpw」の md5 ハッシュです)

于 2009-07-23T05:31:55.253 に答える