我分析了你的代码,发现了错误:你不仅需要异或你的密码,还需要所有64字节
不应解码密钥,因为md5.update()可处理二进制数据
出于同样的原因,您需要调用m1.digest()而不是m1.hexdigest()
您的代码与my fixes和py3k兼容:import hashlib
from base64 import b64encode, b64decode
import sys
def main(nonce):
pwd = bytearray('password'.encode('utf-8'))
key = bytearray(64*b'\x36')
for i in range(len(pwd)):
key[i] ^= pwd[i]
m1 = hashlib.md5()
m1.update(key)
m1.update(b64decode(nonce))
m2 = hashlib.md5()
key = bytearray(64*b'\x5c')
for i in range(len(pwd)):
key[i] ^= pwd[i]
m2.update(key)
m2.update(m1.digest())
response = "username " + m2.hexdigest()
print(b64encode(response.encode('utf-8')).decode('ascii'))
if __name__ == "__main__":
if (len(sys.argv) != 2):
print("ERROR usage: smtp-cram-md5 ")
else:
main(sys.argv[1])
免责声明:
此代码仅对密码长度不超过64字节有效!
(见RFC 2195)