Python身份验证API

2024-04-25 19:24:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个python库,它将帮助我为正在编写的桌面应用程序创建身份验证方法。 我在web框架中发现了一些方法,比如django或turbogears。

我只想将一种用户名密码关联存储到本地文件中。 我可以自己写,但我真的是它已经存在,将是一个更好的解决方案(我不是很流利的加密)。


Tags: 文件django方法框架身份验证web应用程序密码
3条回答

dbr说:

def hash_password(password):
    """Returns the hashed version of a string
    """
    return hasher.new( str(password) ).hexdigest()

这是一种非常不安全的散列密码方法。你不想这么做。如果你想知道为什么要读那些为OpenBSD做密码散列系统的人写的Bycrypt Paper。此外,如果想好好讨论密码是如何破解的,请与开膛手杰克(流行的unix密码破解器)的作者一起查看this interview

现在B-Crypt很好,但是我不得不承认我没有使用这个系统,因为我没有EKS Blowfish算法,我不想自己实现它。我使用的是FreeBSD系统的一个稍微更新的版本,我将在下面发布。关键是这个。不要只是散列密码。将密码加盐,然后散列密码并重复10000次左右。

如果没有意义,这里是代码:

#note I am using the Python Cryptography Toolkit
from Crypto.Hash import SHA256

HASH_REPS = 50000

def __saltedhash(string, salt):
    sha256 = SHA256.new()
    sha256.update(string)
    sha256.update(salt)
    for x in xrange(HASH_REPS): 
        sha256.update(sha256.digest())
        if x % 10: sha256.update(salt)
    return sha256

def saltedhash_bin(string, salt):
    """returns the hash in binary format"""
    return __saltedhash(string, salt).digest()

def saltedhash_hex(string, salt):
    """returns the hash in hex format"""
    return __saltedhash(string, salt).hexdigest()

对于部署这样的系统,需要考虑的关键问题是HASH-REPS常量。这是这个系统中可扩展的成本因素。您将需要进行测试,以确定您希望等待计算每个散列的可容许时间量与基于脱机字典的密码文件攻击的风险。

安全性是很难的,我提出的方法并不是实现这一点的最佳方法,但它比简单的散列要好得多。此外,实现起来非常简单。所以即使你没有选择更复杂的解决方案,这也不是最糟糕的。

希望能帮上忙, 提姆

将以下代码视为伪代码。。

try:
    from hashlib import sha as hasher
except ImportError:
    # You could probably exclude the try/except bit,
    # but older Python distros dont have hashlib.
    try:
        import sha as hasher
    except ImportError:
        import md5 as hasher


def hash_password(password):
    """Returns the hashed version of a string
    """
    return hasher.new( str(password) ).hexdigest()

def load_auth_file(path):
    """Loads a comma-seperated file.
    Important: make sure the username
    doesn't contain any commas!
    """
    # Open the file, or return an empty auth list.
    try:
        f = open(path)
    except IOError:
        print "Warning: auth file not found"
        return {}

    ret = {}
    for line in f.readlines():
        split_line = line.split(",")
        if len(split_line) > 2:
            print "Warning: Malformed line:"
            print split_line
            continue # skip it..
        else:
            username, password = split_line
            ret[username] = password
        #end if
    #end for
    return ret

def main():
    auth_file = "/home/blah/.myauth.txt"
    u = raw_input("Username:")
    p = raw_input("Password:") # getpass is probably better..
    if auth_file.has_key(u.strip()):
        if auth_file[u] == hash_password(p):
            # The hash matches the stored one
            print "Welcome, sir!"

我建议不要使用逗号分隔的文件,而是使用SQLite3(它可以用于其他设置等)。

另外,请记住,这不是很安全-如果应用程序是本地的,邪恶的用户可能只是替换~/.myauth.txt文件。。本地应用程序身份验证很难做好。您必须使用用户密码加密它读取的任何数据,并且通常要非常小心。

我认为您应该创建自己的身份验证方法,因为您可以使它最适合您的应用程序,但使用库进行加密,例如pycrypto或其他一些更轻量级的库。

顺便说一下,如果您需要pycrypto的windows二进制文件,可以获取它们here

相关问题 更多 >