我尝试在其中一个步骤中调用专门使用MD5哈希的API。在文档中,他们特别展示了以以下方式生成MD5的示例引用
$ openssl passwd -1 -salt stack overflow
$1$stack$MVcBmQ3RlrBu5Xoj74NBA0
或者更确切地说,他们只是使用第三个之后的部分
$
$ openssl passwd -1 -salt stack overflow | cut -f 4 -d '$'
MVcBmQ3RlrBu5Xoj74NBA0
起初,我尝试使用
hashlib
并得到了与examplea完全不相似的十六进制输出。
salt = b'stack'
input = b'overflow'
output = hashlib.md5(salt + input).hexdigest()
print(output)
73868cb1848a216984dca1b6b0ee37bc
我想我只需要将这些十六进制值解码为字符,但解码不适用于默认值
utf8
或
latin1
salt = b'stack'
input = b'overflow'
output = hashlib.md5(salt + input).digest().decode()
print(output)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x86 in position 1: invalid start byte
我在这里找到了一些帮助
python version of openssl passwd
在这里
MD5 hash in Python
我可以用
crypt
$ openssl passwd -salt stack overflow
st22n6QiCXNQY
salt = 'stack'
input = 'overflow'
output = crypt.crypt(input, salt)
print(output)
st22n6QiCXNQY
但是只要打开ssl密码
-1
添加,表示
-1 MD5-based password algorithm
我不能再复制了。
如何在Python中重新创建基于MD5的密码算法?我最好使用
hashlib
如果可能的话。