【发布时间】:2014-10-13 19:29:33
【问题描述】:
我正在开发一个Google App Engine 应用程序,我需要对密码进行哈希处理并加盐,为此我使用以下方法:
def hash_password(password):
#Create salt
salt = base64.urlsafe_b64encode(os.urandom(8))
salted = base64.b64encode(base64.urlsafe_b64decode(salt) + password)
hashed = hashlib.md5(salted).digest()
return base64.b64encode(hashed)
问题在于尝试从base64 解码盐时,因为通过 urandom 生成的盐具有non-ascii 字符。结果报错:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd8 in position 2: ordinal not in range(128)
我不能跳过解码base64 上的盐的步骤,因为我已经有一个数据存储,其中包含以这种方式生成的密码和盐,它是通过 java 代码填充的。
我怎样才能保留这个结构,并且仍然获得一个有效的散列密码,我可以用它来与我的数据存储中已有的散列密码进行比较?
当我在终端上使用 python 解释器执行相同的方法时,它运行时没有任何问题,但是当在 GAE 的开发服务器上运行它时(在本地,使用相同的 python 解释器),它会导致该错误。
【问题讨论】:
标签: python google-app-engine base64 non-ascii-characters