多次散列加盐字符串(自定义密码散列)

Hashing salted string multiple times (custom password hashing)

我需要将旧的 Python 2 代码移植到 Python 3,我想我弄乱了字符串编码。

这是一个自定义密码哈希器。

我试过不同的方法,都没有成功,只得到错误或错误的结果。

这是需要与 Python 3 配合使用的 Python 2 代码:

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))