Skip to content

Commit f051062

Browse files
committed
use less reflection
1 parent 912f3e7 commit f051062

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

digest.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,20 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
7777
}
7878
// hash the username if requested
7979
if cred.Userhash {
80-
cred.Username = hashf(h, "%s:%s", o.Username, cred.Realm)
80+
cred.Username = hashjoin(h, o.Username, cred.Realm)
8181
}
8282
// generate the a1 hash if one was not provided
8383
a1 := o.A1
8484
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)
8686
}
8787
// generate the response
8888
switch {
8989
case len(chal.QOP) == 0:
90-
cred.Response = hashf(h, "%s:%s:%s",
90+
cred.Response = hashjoin(h,
9191
a1,
9292
cred.Nonce,
93-
hashf(h, "%s:%s", o.Method, o.URI), // A2
93+
hashjoin(h, o.Method, o.URI), // A2
9494
)
9595
case chal.SupportsQOP("auth"):
9696
cred.QOP = "auth"
@@ -100,13 +100,13 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
100100
if cred.Nc == 0 {
101101
cred.Nc = 1
102102
}
103-
cred.Response = hashf(h, "%s:%s:%08x:%s:%s:%s",
103+
cred.Response = hashjoin(h,
104104
a1,
105105
cred.Nonce,
106-
cred.Nc,
106+
fmt.Sprintf("%08x", cred.Nc),
107107
cred.Cnonce,
108108
cred.QOP,
109-
hashf(h, "%s:%s", o.Method, o.URI), // A2
109+
hashjoin(h, o.Method, o.URI), // A2
110110
)
111111
case chal.SupportsQOP("auth-int"):
112112
cred.QOP = "auth-int"
@@ -120,23 +120,28 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
120120
if err != nil {
121121
return nil, fmt.Errorf("digest: failed to read body for auth-int: %w", err)
122122
}
123-
cred.Response = hashf(h, "%s:%s:%08x:%s:%s:%s",
123+
cred.Response = hashjoin(h,
124124
a1,
125125
cred.Nonce,
126-
cred.Nc,
126+
fmt.Sprintf("%08x", cred.Nc),
127127
cred.Cnonce,
128128
cred.QOP,
129-
hashf(h, "%s:%s:%s", o.Method, o.URI, hbody), // A2
129+
hashjoin(h, o.Method, o.URI, hbody), // A2
130130
)
131131
default:
132132
return nil, fmt.Errorf("digest: unsupported qop: %q", strings.Join(chal.QOP, ","))
133133
}
134134
return cred, nil
135135
}
136136

137-
func hashf(h hash.Hash, format string, args ...interface{}) string {
137+
func hashjoin(h hash.Hash, parts ...string) string {
138138
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+
}
140145
return hex.EncodeToString(h.Sum(nil))
141146
}
142147

0 commit comments

Comments
 (0)