def _authenticate_cram_md5(credentials, sock_info):
 source = credentials.source
 username = credentials.username
 password = credentials.password
 passwd = _password_digest(username, password)
 cmd = SON([('saslStart', 1), ('mechanism', 'CRAM-MD5'), ('payload', Binary('')), ('autoAuthorize', 1)])
 response = sock_info.command(source, cmd)
 mac = hmac.HMAC(key=passwd.encode('utf-8'), digestmod=md5)
 mac.update(response['payload'])
 challenge = ((username.encode('utf-8') + ' ') + b(mac.hexdigest()))
 cmd = SON([('saslContinue', 1), ('conversationId', response['conversationId']), ('payload', Binary(challenge))])
 sock_info.command(source, cmd)

def _authenticate_cram_md5 ( credentials , sock_info ) : source = credentials . source username = credentials . username password = credentials . password passwd = _password_digest ( username , password ) cmd = SON ( [ ( 'saslStart' , 1 ) , ( 'mechanism' , 'CRAM-MD5' ) , ( 'payload' , Binary ( '' ) ) , ( 'autoAuthorize' , 1 ) ] ) response = sock_info . command ( source , cmd ) mac = hmac . HMAC ( key = passwd . encode ( 'utf-8' ) , digestmod = md5 ) mac . update ( response [ 'payload' ] ) challenge = ( ( username . encode ( 'utf-8' ) + ' ' ) + b ( mac . hexdigest ( ) ) ) cmd = SON ( [ ( 'saslContinue' , 1 ) , ( 'conversationId' , response [ 'conversationId' ] ) , ( 'payload' , Binary ( challenge ) ) ] ) sock_info . command ( source , cmd )
authenticate using cram-md5 .