Ber*_*and 6 python authentication desktop
我正在寻找一个python库,它将帮助我为我正在编写的桌面应用程序创建一个身份验证方法.我在web框架中找到了几种方法,如django或turbogears.
我只想要一种存储在本地文件中的用户名 - 密码关联.我可以自己写,但我真的它已经存在并且将是一个更好的解决方案(我对加密不是很流利).
tim*_*adh 10
dbr说:
Run Code Online (Sandbox Code Playgroud)def hash_password(password): """Returns the hashed version of a string """ return hasher.new( str(password) ).hexdigest()
这是一种非常不安全的哈希密码方法.你不希望这样做.如果你想知道为什么要为那些为OpenBSD做密码散列系统的人阅读Bycrypt Paper.此外,如果想要对密码如何破解进行讨论,请查看Jack the Ripper(流行的unix密码破解程序)作者的访谈.
现在B-Crypt很棒,但我必须承认我不使用这个系统,因为我没有EKS-Blowfish算法可用,并且不想自己实现它.我使用稍微更新的FreeBSD系统版本,我将在下面发布.要点就是这个.不要只是哈希密码.解密密码然后散列密码并重复10,000次左右.
如果这没有意义,那么代码是:
#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()
Run Code Online (Sandbox Code Playgroud)
对于部署这样的系统,需要考虑的关键是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!"
Run Code Online (Sandbox Code Playgroud)
我建议使用 SQLite3(可用于其他设置等),而不是使用逗号分隔的文件。
另外,请记住,这不是很安全 - 如果应用程序是本地的,邪恶的用户可能只是替换该~/.myauth.txt文件。本地应用程序身份验证很难做好。您必须使用用户密码对其读取的任何数据进行加密,并且通常要非常小心。
| 归档时间: |
|
| 查看次数: |
9860 次 |
| 最近记录: |