Following the guideline given in SMTP with CRAM-MD5 in Java I wrote a small program in Python to calculate the response when given the nonce as input:
import hashlib
from base64 import b64encode, b64decode
import sys
from decimal import *
#MD5(('secret' XOR opad), MD5(('secret' XOR ipad), challenge))
#opad - 0x5C, ipad - 0x36.
def main(nonce):
pwd = bytearray("password")
for i in range(len(pwd)):
pwd[i] = pwd[i] ^ 0x36
m1 = hashlib.md5()
m1.update(pwd.decode())
m1.update(b64decode(nonce))
m2 = hashlib.md5()
pwd = bytearray("password")
for i in range(len(pwd)):
pwd[i] = pwd[i] ^ 0x5C
m2.update(pwd.decode())
m2.update(m1.hexdigest())
print b64encode("username " + m2.hexdigest())
if __name__ == "__main__":
if (len(sys.argv) != 2):
print("ERROR usage: smtp-cram-md5 <nonce>")
else:
main(sys.argv[1])
However, the SMTP server rejects the response I give generated by this program. Can some one please point out what I am doing wrong?