forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigests.go
53 lines (43 loc) · 1.13 KB
/
digests.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
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2021 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"fmt"
"github.com/veraison/swid"
)
// Digests is an alias for an array of SWID HashEntry
type Digests []swid.HashEntry
// NewDigests instantiates an empty array of Digests
func NewDigests() *Digests {
return new(Digests)
}
// AddDigest create a new digest from the supplied arguments and appends it to
// the (already instantiated) Digests target. The method is a no-op if it is
// invoked on a nil target and will refuse to add inconsistent algo/value
// combinations.
func (o *Digests) AddDigest(algID uint64, value []byte) *Digests {
if o != nil {
he := NewHashEntry(algID, value)
if he == nil {
return nil
}
*o = append(*o, *he)
}
return o
}
func (o Digests) Valid() error {
for i, m := range o {
if err := swid.ValidHashEntry(m.HashAlgID, m.HashValue); err != nil {
return fmt.Errorf("digest at index %d: %w", i, err)
}
}
return nil
}
func NewHashEntry(algID uint64, value []byte) *swid.HashEntry {
var he swid.HashEntry
err := he.Set(algID, value)
if err != nil {
return nil
}
return &he
}