Skip to content

Commit b1b6f62

Browse files
committed
formatting & doc
1 parent 7a207b9 commit b1b6f62

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

digest.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ type Options struct {
2727
URI string
2828
GetBody func() (io.ReadCloser, error)
2929
Count int
30-
A1 string
3130
Username string
3231
Password string
3332

34-
// used for testing
33+
// The following are provided for advanced use cases where the client needs
34+
// to override the default digest calculation behavior. Most users should
35+
// leave these fields unset.
36+
A1 string
3537
Cnonce string
3638
}
3739

@@ -77,18 +79,16 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
7779
if cred.Userhash {
7880
cred.Username = hashf(h, "%s:%s", o.Username, cred.Realm)
7981
}
80-
82+
// generate the a1 hash if one was not provided
8183
a1 := o.A1
82-
8384
if a1 == "" {
8485
a1 = hashf(h, "%s:%s:%s", o.Username, cred.Realm, o.Password)
8586
}
86-
8787
// generate the response
8888
switch {
8989
case len(chal.QOP) == 0:
9090
cred.Response = hashf(h, "%s:%s:%s",
91-
a1, // A1
91+
a1,
9292
cred.Nonce,
9393
hashf(h, "%s:%s", o.Method, o.URI), // A2
9494
)
@@ -101,7 +101,7 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
101101
cred.Nc = 1
102102
}
103103
cred.Response = hashf(h, "%s:%s:%08x:%s:%s:%s",
104-
a1, // A1
104+
a1,
105105
cred.Nonce,
106106
cred.Nc,
107107
cred.Cnonce,
@@ -121,7 +121,7 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
121121
return nil, fmt.Errorf("digest: failed to read body for auth-int: %w", err)
122122
}
123123
cred.Response = hashf(h, "%s:%s:%08x:%s:%s:%s",
124-
a1, // A1
124+
a1,
125125
cred.Nonce,
126126
cred.Nc,
127127
cred.Cnonce,

digest_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,17 @@ func TestDigestSHA256(t *testing.T) {
112112
}
113113

114114
func TestDigestA1(t *testing.T) {
115+
h := sha256.New()
116+
fmt.Fprintf(h, "%s:%s:%s",
117+
"Mufasa", // username
118+
"http-auth@example.org", // realm
119+
"Circle of Life", // password
120+
)
121+
a1 := hex.EncodeToString(h.Sum(nil))
115122
opt := Options{
116123
Method: "GET",
117124
URI: "/dir/index.html",
118-
A1: sha265Hash("%s:%s:%s", "Mufasa", "http-auth@example.org", "Circle of Life"),
125+
A1: a1,
119126
Cnonce: "f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ",
120127
}
121128
chal := &Challenge{
@@ -204,9 +211,3 @@ func TestDigestAuthInt(t *testing.T) {
204211
Nc: 1,
205212
})
206213
}
207-
208-
func sha265Hash(format string, args ...interface{}) string {
209-
h := sha256.New()
210-
fmt.Fprintf(h, format, args...)
211-
return hex.EncodeToString(h.Sum(nil))
212-
}

0 commit comments

Comments
 (0)