我写了一个小瓶子网络应用程序运行在我的树莓派和控制一个锅炉。有一个登录页面和一个创建新用户页面。当我创建一个新用户时,它生成一个salt并使用sha512散列密码,这两个都存储在数据库中。当用户登录时,它与用户标识相匹配,并从数据库中获取salt和密码哈希,并使用数据库中的salt对所提供的密码进行哈希处理,但总是创建一个不同的哈希,因此登录失败。我敢肯定这是件蠢事,但我就是分不清。在
这是执行密码散列/检查/salt的代码
def get_password(userid):
userid = userid.upper()
logging.debug('get password for %s' % userid)
conn_string = prop('database')
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
sql = """
select password, salt from users where userid = %(userid)s
"""
cursor.execute(sql, {'userid':userid})
row = cursor.fetchone()
if row is not None:
dbpassword = row[0]
dbsalt = str(row[1])
logging.debug('db password hash %s' % dbpassword)
logging.debug('db password salt %s' % dbsalt)
return dbpassword, dbsalt
else:
logging.debug('No details found for user')
return None, None
def check_password(password, userid):
logging.debug('username/password to check is %s/%s' % (password, userid))
dbpassword, dbsalt = get_password(userid)
if dbpassword is not None:
test = hash_password(password, dbsalt)
logging.debug('test password hash %s' % test)
if test == dbpassword:
logging.debug('password correct')
return True
else:
logging.debug('password incorrect')
return False
else:
return False
def hash_password(password, salt):
if salt == '0':
logging.debug('hashing password')
logging.debug('generate salt')
salt = uuid.uuid4().hex
logging.debug('salt = %s' % salt)
hashed_password = crypt(password, salt)
logging.debug('hashed password = %s' % hashed_password)
return salt, hashed_password
else:
logging.debug('hash password for compare')
hashed_password = crypt(password, salt)
logging.debug('hashed password = %s' % hashed_password)
return hashed_password
def crypt(password, salt):
hashed_password = hashlib.sha512(password.encode(encoding='utf_8') + salt.encode(encoding='utf_8')).hexdigest()
return hashed_password
这是从登录页面获取详细信息的部分:
^{pr2}$这将从新用户页面获取详细信息:
def new_user():
try:
rqstSession = request.get_cookie('pysessionid', secret=prop('cookieSecret'))
if check_session(rqstSession) is True:
if request.forms.get('save','').strip():
userid = request.forms.get('userid', '').upper()
password = request.forms.get('password','')
confpassword = request.forms.get('confpassword','')
salt = '0'
if password is not '' and password == confpassword and userid is not '':
salt, hashed_password = hash_password(userid, salt)
conn_string = prop('database')
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
sql = """
insert into users (id_usrr, userid, password, salt) values (nextval('users_id_usrr_seq'), %(userid)s, %(password)s, %(salt)s)
"""
cursor.execute(sql, {'userid':userid, 'password':hashed_password, 'salt':salt})
conn.commit()
cursor.close()
else:
return template('newuser')
else:
return template('newuser')
else:
pysessionid = ''
response.set_cookie('pysessionid', pysessionid, secret=prop('cookieSecret'), Expires='Thu, 01-Jan-1970 00:00:10 GMT', httponly=True)
return template('main')
except Exception as e:
logging.debug(e)
return '<p>Error</p>'
我试着去掉盐,但没用,所以我不认为这和那有什么关系,但在我头撞到墙上两个小时后,我愿意尝试任何东西
谢谢 亚当
我对安全不太了解,但我认为这可以解决你的问题
比如说:
^{pr2}$passlib
相关问题 更多 >
编程相关推荐