/*
服务端发送:
<challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">bm9uY2U9IjEyOTE2MjcyODIwOTg3MTg0MzYzIixxb3A9ImF1dGgiLGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz</challenge>
其中bm9uY2U9IjEyOTE2MjcyODIwOTg3MTg0MzYzIixxb3A9ImF1dGgiLGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz base64解码后是:
nonce="12916272820987184363",qop="auth",charset=utf-8,algorithm=md5-sess
*/
/*
客户端响应:
<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">dXNlcm5hbWU9ImFhYWFhYSIsbm9uY2U9IjEyOTE2MjcyODIwOTg3MTg0MzYzIixjbm9uY2U9IkM2QUMvS1ZycWxaUFNLa3RmdmJXUWJvbTBSdUtTYXZXVHFuSzhPNzJYS3M9IixuYz0wMDAwMDAwMSxkaWdlc3QtdXJpPSJ4bXBwLzExMi43NC42Ny45MiIscW9wPWF1dGgscmVzcG9uc2U9MDc4NDNkZjNlMTQ0ZWRhOTY1ZjUzNWQzMzEyZTJhNzgsY2hhcnNldD11dGYtOA==</response>
base64解码:
username="aaaaaa",nonce="12916272820987184363",cnonce="C6AC/KVrqlZPSKktfvbWQbom0RuKSavWTqnK8O72XKs=",nc=00000001,digest-uri="xmpp/112.74.67.92",qop=auth,response=07843df3e144eda965f535d3312e2a78,charset=utf-8
username是用户名
nonce是上面服务端发过来的nonce
conce是客户端随机字串
nc 是8位长的16进制数字符串,统计客户端使用nonce发出请求的次数(包含当前请求),例示我们可以设为”00000001”,
digest-uri=“xmpp/hostname”
qop=auth
response 按下面的算法response()计算
*/
package main
import "fmt"
import "crypto/md5"
import "encoding/hex"
func main() {
response()
}
func strMd5(x string) string {
h := md5.New()
h.Write([]byte(x)) // 需要加密的字符串为 sharejs.com
return hex.EncodeToString(h.Sum(nil)) // 输出加密结果
}
func response() {
User := "aaaaaa"
A2Prefix:= "AUTHENTICATE"
Passwd:= "111111"
Realm := ""
CNonce := "C6AC/KVrqlZPSKktfvbWQbom0RuKSavWTqnK8O72XKs="
Nonce:= "12916272820987184363"
DigestURI := "xmpp/112.74.67.92"
NC := "00000001"
QOP := "auth"
h := md5.New()
h.Write([]byte(User+":"+Realm+":"+Passwd))
MD5Hash := string(h.Sum(nil))
A1 := MD5Hash + ":"+ Nonce+ ":"+CNonce
A2 := A2Prefix+":"+ DigestURI
T := strMd5(A1) + ":" + Nonce + ":"+NC+":"+ CNonce+":"+ QOP+ ":"+strMd5(A2)
fmt.Print(strMd5(T)+"\n")
}