forked from jbowes/httpsig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest.go
42 lines (31 loc) · 1.03 KB
/
digest.go
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
31
32
33
34
35
36
37
38
39
40
41
42
// 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/sha512"
"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.
// type Digester interface {
// ContentDigest (in []byte) string
// }
func ContentDigesSHA216(in []byte) string {
dig := sha256.Sum256(in)
return fmt.Sprintf("id-sha256=%s", base64.StdEncoding.EncodeToString(dig[:]))
}
func calcDigest(in []byte) string {
// Hash the input
digest := sha512.Sum512(in)
return fmt.Sprintf("sha-512=:%s:", base64.StdEncoding.EncodeToString(digest[:]))
}
func verifyDigest(in []byte, dig string) bool {
// TODO: case insensitity for incoming digest?
calc := calcDigest(in)
return subtle.ConstantTimeCompare([]byte(dig), []byte(calc)) == 1
}