Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.

Commit 12e4990

Browse files
committed
case insensitive algorithm name
1 parent 89e5f25 commit 12e4990

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

authorization.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func newAuthorization(dr *DigestRequest) (*authorization, error) {
4949
return ah.refreshAuthorization(dr)
5050
}
5151

52+
const (
53+
algorithmMD5 = "MD5"
54+
algorithmMD5Sess = "MD5-SESS"
55+
algorithmSHA256 = "SHA-256"
56+
algorithmSHA256Sess = "SHA-256-SESS"
57+
)
58+
5259
func (ah *authorization) refreshAuthorization(dr *DigestRequest) (*authorization, error) {
5360

5461
ah.Username = dr.Username
@@ -82,11 +89,13 @@ func (ah *authorization) computeResponse(dr *DigestRequest) (s string) {
8289

8390
func (ah *authorization) computeA1(dr *DigestRequest) string {
8491

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 {
8695
return fmt.Sprintf("%s:%s:%s", ah.Username, ah.Realm, dr.Password)
8796
}
8897

89-
if ah.Algorithm == "MD5-sess" || ah.Algorithm == "SHA-256-sess" {
98+
if algorithm == algorithmMD5Sess || algorithm == algorithmSHA256Sess {
9099
upHash := ah.hash(fmt.Sprintf("%s:%s:%s", ah.Username, ah.Realm, dr.Password))
91100
return fmt.Sprintf("%s:%s:%s", upHash, ah.Nonce, ah.Cnonce)
92101
}
@@ -109,20 +118,21 @@ func (ah *authorization) computeA2(dr *DigestRequest) string {
109118
return ""
110119
}
111120

112-
func (ah *authorization) hash(a string) (s string) {
113-
121+
func (ah *authorization) hash(a string) string {
114122
var h hash.Hash
123+
algorithm := strings.ToUpper(ah.Algorithm)
115124

116-
if ah.Algorithm == "" || ah.Algorithm == "MD5" || ah.Algorithm == "MD5-sess" {
125+
if algorithm == "" || algorithm == algorithmMD5 || algorithm == algorithmMD5Sess {
117126
h = md5.New()
118-
} else if ah.Algorithm == "SHA-256" || ah.Algorithm == "SHA-256-sess" {
127+
} else if algorithm == algorithmSHA256 || algorithm == algorithmSHA256Sess {
119128
h = sha256.New()
129+
} else {
130+
// unknown algorithm
131+
return ""
120132
}
121133

122134
io.WriteString(h, a)
123-
s = hex.EncodeToString(h.Sum(nil))
124-
125-
return
135+
return hex.EncodeToString(h.Sum(nil))
126136
}
127137

128138
func (ah *authorization) toString() string {

authorization_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ func TestHash(t *testing.T) {
3333
algorithm: "SHA-256",
3434
expRes: "50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c",
3535
},
36-
//{
37-
// name: "md5 algorithm will panic",
38-
// algorithm: "md5",
39-
// expRes: "1a79a4d60de6718e8e5b326e338ae533",
40-
//},
41-
//{
42-
// name: "unknown algorithm will panic",
43-
// algorithm: "unknown",
44-
// expRes: "",
45-
//},
36+
{
37+
name: "md5 algorithm",
38+
algorithm: "md5",
39+
expRes: "1a79a4d60de6718e8e5b326e338ae533",
40+
},
41+
{
42+
name: "unknown algorithm",
43+
algorithm: "unknown",
44+
expRes: "",
45+
},
4646
}
4747

4848
for _, tc := range testCases {
@@ -90,7 +90,7 @@ func TestComputeA1(t *testing.T) {
9090
{
9191
name: "md5 algorithm",
9292
algorithm: "md5",
93-
expRes: "",
93+
expRes: "username:realm:secret",
9494
},
9595
{
9696
name: "unknown algorithm",

0 commit comments

Comments
 (0)