@@ -49,6 +49,13 @@ func newAuthorization(dr *DigestRequest) (*authorization, error) {
49
49
return ah .refreshAuthorization (dr )
50
50
}
51
51
52
+ const (
53
+ algorithmMD5 = "MD5"
54
+ algorithmMD5Sess = "MD5-SESS"
55
+ algorithmSHA256 = "SHA-256"
56
+ algorithmSHA256Sess = "SHA-256-SESS"
57
+ )
58
+
52
59
func (ah * authorization ) refreshAuthorization (dr * DigestRequest ) (* authorization , error ) {
53
60
54
61
ah .Username = dr .Username
@@ -82,11 +89,13 @@ func (ah *authorization) computeResponse(dr *DigestRequest) (s string) {
82
89
83
90
func (ah * authorization ) computeA1 (dr * DigestRequest ) string {
84
91
85
- if ah .Algorithm == "" || ah .Algorithm == "MD5" || ah .Algorithm == "SHA-256" {
92
+ algorithm := strings .ToUpper (ah .Algorithm )
93
+
94
+ if algorithm == "" || algorithm == algorithmMD5 || algorithm == algorithmSHA256 {
86
95
return fmt .Sprintf ("%s:%s:%s" , ah .Username , ah .Realm , dr .Password )
87
96
}
88
97
89
- if ah . Algorithm == "MD5-sess" || ah . Algorithm == "SHA-256-sess" {
98
+ if algorithm == algorithmMD5Sess || algorithm == algorithmSHA256Sess {
90
99
upHash := ah .hash (fmt .Sprintf ("%s:%s:%s" , ah .Username , ah .Realm , dr .Password ))
91
100
return fmt .Sprintf ("%s:%s:%s" , upHash , ah .Nonce , ah .Cnonce )
92
101
}
@@ -109,20 +118,21 @@ func (ah *authorization) computeA2(dr *DigestRequest) string {
109
118
return ""
110
119
}
111
120
112
- func (ah * authorization ) hash (a string ) (s string ) {
113
-
121
+ func (ah * authorization ) hash (a string ) string {
114
122
var h hash.Hash
123
+ algorithm := strings .ToUpper (ah .Algorithm )
115
124
116
- if ah . Algorithm == "" || ah . Algorithm == "MD5" || ah . Algorithm == "MD5-sess" {
125
+ if algorithm == "" || algorithm == algorithmMD5 || algorithm == algorithmMD5Sess {
117
126
h = md5 .New ()
118
- } else if ah . Algorithm == "SHA-256" || ah . Algorithm == "SHA-256-sess" {
127
+ } else if algorithm == algorithmSHA256 || algorithm == algorithmSHA256Sess {
119
128
h = sha256 .New ()
129
+ } else {
130
+ // unknown algorithm
131
+ return ""
120
132
}
121
133
122
134
io .WriteString (h , a )
123
- s = hex .EncodeToString (h .Sum (nil ))
124
-
125
- return
135
+ return hex .EncodeToString (h .Sum (nil ))
126
136
}
127
137
128
138
func (ah * authorization ) toString () string {
0 commit comments