-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserdata.go
More file actions
39 lines (33 loc) · 772 Bytes
/
userdata.go
File metadata and controls
39 lines (33 loc) · 772 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
31
32
33
34
35
36
37
38
39
package attestation
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
)
const (
_UserDataPrefix = "Sequence"
_UserDataVersion = 1
)
type userData struct {
Prefix string
Version int
Hash []byte
}
func (u *userData) String() string {
return fmt.Sprintf("%s/%d:%s", u.Prefix, u.Version, base64.StdEncoding.EncodeToString(u.Hash))
}
func generateUserData(r *http.Request, reqBody []byte, resBody []byte) ([]byte, error) {
hasher := sha256.New()
hasher.Write([]byte(r.Method + " " + r.URL.Path + "\n"))
hasher.Write(reqBody)
hasher.Write([]byte("\n"))
hasher.Write(resBody)
hash := hasher.Sum(nil)
userData := &userData{
Prefix: _UserDataPrefix,
Version: _UserDataVersion,
Hash: hash,
}
return []byte(userData.String()), nil
}