@@ -2,6 +2,7 @@ package auth
2
2
3
3
import (
4
4
"crypto/md5"
5
+ "crypto/sha256"
5
6
"encoding/hex"
6
7
"fmt"
7
8
@@ -12,11 +13,13 @@ import (
12
13
const Realm = "sing-box"
13
14
14
15
type Challenge struct {
15
- Username string
16
- Nonce string
17
- CNonce string
18
- Nc string
19
- Response string
16
+ Username string
17
+ Nonce string
18
+ Algorithm string
19
+ Uri string
20
+ CNonce string
21
+ Nc string
22
+ Response string
20
23
}
21
24
22
25
type User struct {
@@ -54,13 +57,23 @@ func (au *Authenticator) VerifyDigest(method string, uri string, s string) (stri
54
57
if c .Username == "" || c .Nonce == "" || c .Nc == "" || c .CNonce == "" || c .Response == "" {
55
58
return "" , false
56
59
}
60
+ if c .Uri != "" {
61
+ uri = c .Uri
62
+ }
57
63
passwordList , ok := au .userMap [c .Username ]
58
64
if ok {
59
65
for _ , password := range passwordList {
60
- ha1 := md5str (c .Username + ":" + Realm + ":" + password )
61
- ha2 := md5str (method + ":" + uri )
62
- resp := md5str (ha1 + ":" + c .Nonce + ":" + c .Nc + ":" + c .CNonce + ":auth:" + ha2 )
63
- if resp == c .Response {
66
+ resp := ""
67
+ if c .Algorithm == "SHA-256" {
68
+ ha1 := sha256str (c .Username + ":" + Realm + ":" + password )
69
+ ha2 := sha256str (method + ":" + uri )
70
+ resp = sha256str (ha1 + ":" + c .Nonce + ":" + c .Nc + ":" + c .CNonce + ":auth:" + ha2 )
71
+ } else {
72
+ ha1 := md5str (c .Username + ":" + Realm + ":" + password )
73
+ ha2 := md5str (method + ":" + uri )
74
+ resp = md5str (ha1 + ":" + c .Nonce + ":" + c .Nc + ":" + c .CNonce + ":auth:" + ha2 )
75
+ }
76
+ if resp != "" && resp == c .Response {
64
77
return c .Username , true
65
78
}
66
79
}
@@ -81,6 +94,10 @@ func ParseChallenge(s string) (*Challenge, error) {
81
94
c .Username = p .Value
82
95
case "nonce" :
83
96
c .Nonce = p .Value
97
+ case "algorithm" :
98
+ c .Algorithm = p .Value
99
+ case "uri" :
100
+ c .Uri = p .Value
84
101
case "cnonce" :
85
102
c .CNonce = p .Value
86
103
case "nc" :
@@ -97,3 +114,9 @@ func md5str(str string) string {
97
114
h .Write ([]byte (str ))
98
115
return hex .EncodeToString (h .Sum (nil ))
99
116
}
117
+
118
+ func sha256str (str string ) string {
119
+ h := sha256 .New ()
120
+ h .Write ([]byte (str ))
121
+ return hex .EncodeToString (h .Sum (nil ))
122
+ }
0 commit comments