我需要将旧的Python 2代码移植到Python 3中,我想我的字符串编码出了问题。
这是一个自定义的密码散列器。
我尝试了不同的方法,都不成功,只得到错误或错误的结果。
这是Python 2的代码,它需要与Python 3一起工作。
from hashlib import sha256
from base64 import b64encode
# 32 characters length string
SALT = "SQ7HqXQhrOIPEALbI7QhVjZ3DHJGhK18"
PLAIN_PASSWORD = "PLAIN_PASSWORD"
SALTED_PASSWORD = "%s{%s}" % (PLAIN_PASSWORD, SALT)
digest = ""
for i in range(100):
digest = sha256(digest + SALTED_PASSWORD).digest()
print b64encode(digest)
输出
Yb0W9H+R7xQDStPfBjKMjFbe05jDPK6OXrdhVWCDJrU=
从头开始对字节进行操作。
SALTED_PASSWORD = ("%s{%s}" % (PLAIN_PASSWORD, SALT)).encode()
digest = b""
for i in range(100):
digest = sha256(digest + SALTED_PASSWORD).digest()
print(b64encode(digest).decode())
# Yb0W9H+R7xQDStPfBjKMjFbe05jDPK6OXrdhVWCDJrU=
from hashlib import sha256
from base64 import b64encode
# 32 characters length string
SALT = b"SQ7HqXQhrOIPEALbI7QhVjZ3DHJGhK18"
PLAIN_PASSWORD = b"PLAIN_PASSWORD"
SALTED_PASSWORD = b"%s{%s}" % (PLAIN_PASSWORD, SALT)
digest = b""
for i in range(100):
digest = sha256(digest + SALTED_PASSWORD).digest()
print(b64encode(digest))