在 Python 中实现 Digest-MD5。试图对它的工作原理有一个基本的了解,所以我敲了一些快速代码来看看。它似乎工作并且似乎与 RFC 保持一致,但我总是只收到“失败”消息。这是我的代码:
nonce = [FROM REQUEST]
username = ***
password = ***
server = chat.facebook.com
cnonce = "X8fwG1eWR+EHEVEB44t5qThEpCn/3KqcftmvSyWS3sN="
nc = 000000001
x = "%s:%s:%s" % (self.username, self.server, self.password)
md5sum = md5.new()
md5sum.update(x)
y = (md5sum.digest()).encode('hex')
a = "%s:%s:%s:" % (y, nonce, cnonce)
a2 = "AUTHENTICATE:xmpp/%s" % self.server
md5sum = md5.new()
md5sum.update(a)
ha1 = (md5sum.digest()).encode('hex')
md5sum = md5.new()
md5sum.update(a2)
ha2 = (md5sum.digest()).encode('hex')
kd = "%s:%s:%s:%s:auth:%s" % (ha1, nonce, nc cnonce, ha2)
md5sum = md5.new()
md5sum.update(kd)
z = (md5sum.digest()).encode('hex')
buffer = "username=\"%s\",realm=\"%s\",nonce=\"%s\",cnonce=\"%s\",nc=%s,qop=auth,digest-uri=\"xmpp/%s\",response=%s,charset=utf-8" % (username, server, nonce, cnonce, nc, server, z)
send_data = base64.b64encode(buffer)
这是交换的粗略概述:
Server: <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cmVhbG09ImNoYXQuZmFjZWJvb2suY29tIixub25jZT0iM0E5NjdDQTFFQ0EzM0RDNDdDQzJDQkM0QUQxNjcyMTUiLHFvcD0iYXV0aCIsY2hhcnNldD11dGYtOCxhbGdvcml0aG09bWQ1LXNlc3M=</challenge>
Client: <response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dXNlcm5hbWU9InRlc3RjaGF0MjUwMiIscmVhbG09ImNoYXQuZmFjZWJvb2suY29tIixub25jZT0iM0E5NjdDQTFFQ0EzM0RDNDdDQzJDQkM0QUQxNjcyMTUiLGNub25jZT0iWDlmd0cxZVdSK0VIRVZFQjQ0dDVxVGhFcENuLzNLcWNmdG12U3lXUzNzTj0iLG5jPTAwMDAwMDAxLHFvcD1hdXRoLGRpZ2VzdC11cmk9InhtcHAvY2hhdC5mYWNlYm9vay5jb20iLHJlc3BvbnNlPTQ0ZTQ3MDkzN2RiZDRlM2E2YTBhY2EwN2IyMDM1YWI3LGNoYXJzZXQ9dXRmLTg=</response>
Server: <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
有任何想法吗?