echo -n "password"|openssl dgst -md5 -binary|openssl enc -base64

echo -n "password"|openssl dgst -md5 -binary|openssl enc -base64

当我使用 shell 命令时:

echo -n "password"|openssl dgst -md5 -binary|openssl enc -base64

结果是:X03MO1qnZdYdgyfeuILPmQ==

但是,在python3

我该怎么办
import base64, sys, hashlib
text = "password"
try:
    text = bytes(text, 'utf-8')
except:
    pass
auth = str.encode(hashlib.md5(text).hexdigest())
query = base64.b64encode(auth)
print(query)

结果不同:NWY0ZGNjM2I1YWE3NjVkNjFkODMyN2RlYjg4MmNmOTk=

您在将“-binary”选项传递给 openssl 时请求二进制格式的摘要输出。因此,您还必须处理 python 中的二进制数据。只需使用 digest() 方法而不是 hexdigest():

import base64, sys, hashlib
text = "password"
try:
    text = bytes(text, 'utf-8')
except:
    pass
auth = hashlib.md5(text).digest()
query = base64.b64encode(auth)
print(query.decode())