-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdigest.go
More file actions
30 lines (22 loc) · 785 Bytes
/
digest.go
File metadata and controls
30 lines (22 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (c) 2021 James Bowes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httpsig
import (
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"fmt"
)
// HTTP digest headers support according to the draft standard
// https://datatracker.ietf.org/doc/draft-ietf-httpbis-digest-headers/
// TODO: support more algorithms, and maybe do its own package.
func calcDigest(in []byte) string {
dig := sha256.Sum256(in)
return fmt.Sprintf("id-sha256=%s", base64.StdEncoding.EncodeToString(dig[:]))
}
func verifyDigest(in []byte, dig string) bool {
// TODO: case insensitity for incoming digest?
calc := calcDigest(in)
return subtle.ConstantTimeCompare([]byte(dig), []byte(calc)) == 1
}