@@ -77,20 +77,20 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
77
77
}
78
78
// hash the username if requested
79
79
if cred .Userhash {
80
- cred .Username = hashf ( h , "%s:%s" , o .Username , cred .Realm )
80
+ cred .Username = hashjoin ( h , o .Username , cred .Realm )
81
81
}
82
82
// generate the a1 hash if one was not provided
83
83
a1 := o .A1
84
84
if a1 == "" {
85
- a1 = hashf ( h , "%s:%s:%s" , o .Username , cred .Realm , o .Password )
85
+ a1 = hashjoin ( h , o .Username , cred .Realm , o .Password )
86
86
}
87
87
// generate the response
88
88
switch {
89
89
case len (chal .QOP ) == 0 :
90
- cred .Response = hashf ( h , "%s:%s:%s" ,
90
+ cred .Response = hashjoin ( h ,
91
91
a1 ,
92
92
cred .Nonce ,
93
- hashf ( h , "%s:%s" , o .Method , o .URI ), // A2
93
+ hashjoin ( h , o .Method , o .URI ), // A2
94
94
)
95
95
case chal .SupportsQOP ("auth" ):
96
96
cred .QOP = "auth"
@@ -100,13 +100,13 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
100
100
if cred .Nc == 0 {
101
101
cred .Nc = 1
102
102
}
103
- cred .Response = hashf ( h , "%s:%s:%08x:%s:%s:%s" ,
103
+ cred .Response = hashjoin ( h ,
104
104
a1 ,
105
105
cred .Nonce ,
106
- cred .Nc ,
106
+ fmt . Sprintf ( "%08x" , cred .Nc ) ,
107
107
cred .Cnonce ,
108
108
cred .QOP ,
109
- hashf ( h , "%s:%s" , o .Method , o .URI ), // A2
109
+ hashjoin ( h , o .Method , o .URI ), // A2
110
110
)
111
111
case chal .SupportsQOP ("auth-int" ):
112
112
cred .QOP = "auth-int"
@@ -120,23 +120,28 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
120
120
if err != nil {
121
121
return nil , fmt .Errorf ("digest: failed to read body for auth-int: %w" , err )
122
122
}
123
- cred .Response = hashf ( h , "%s:%s:%08x:%s:%s:%s" ,
123
+ cred .Response = hashjoin ( h ,
124
124
a1 ,
125
125
cred .Nonce ,
126
- cred .Nc ,
126
+ fmt . Sprintf ( "%08x" , cred .Nc ) ,
127
127
cred .Cnonce ,
128
128
cred .QOP ,
129
- hashf ( h , "%s:%s:%s" , o .Method , o .URI , hbody ), // A2
129
+ hashjoin ( h , o .Method , o .URI , hbody ), // A2
130
130
)
131
131
default :
132
132
return nil , fmt .Errorf ("digest: unsupported qop: %q" , strings .Join (chal .QOP , "," ))
133
133
}
134
134
return cred , nil
135
135
}
136
136
137
- func hashf (h hash.Hash , format string , args ... interface {} ) string {
137
+ func hashjoin (h hash.Hash , parts ... string ) string {
138
138
h .Reset ()
139
- fmt .Fprintf (h , format , args ... )
139
+ for i , part := range parts {
140
+ if i > 0 {
141
+ h .Write ([]byte (":" ))
142
+ }
143
+ h .Write ([]byte (part ))
144
+ }
140
145
return hex .EncodeToString (h .Sum (nil ))
141
146
}
142
147
0 commit comments