@@ -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+
5259func (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
8390func (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
128138func (ah * authorization ) toString () string {
0 commit comments