forked from zyedidia/eget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha256_verifier.go
56 lines (48 loc) · 1.22 KB
/
sha256_verifier.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
54
55
56
package verifiers
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/permafrost-dev/eget/lib/assets"
"github.com/permafrost-dev/eget/lib/download"
)
type Sha256Verifier struct {
Expected []byte
client download.ClientContract
Asset *assets.Asset
Verifier
}
func NewSha256Verifier(client download.ClientContract, expectedHex string) (*Sha256Verifier, error) {
expected, _ := hex.DecodeString(expectedHex)
if len(expected) != sha256.Size {
return nil, fmt.Errorf("sha256sum (%s) too small: %d bytes decoded", expectedHex, len(expectedHex))
}
return &Sha256Verifier{
client: client,
Expected: expected,
Asset: nil,
}, nil
}
func (s256 *Sha256Verifier) GetAsset() *assets.Asset {
return s256.Asset
}
func (s256 *Sha256Verifier) WithClient(client download.ClientContract) Verifier {
s256.client = client
var intf interface{} = s256
var result Verifier = intf.(Verifier)
return result
}
func (s256 *Sha256Verifier) Verify(b []byte) error {
sum := sha256.Sum256(b)
if bytes.Equal(sum[:], s256.Expected) {
return nil
}
return &Sha256Error{
Expected: s256.Expected,
Got: sum[:],
}
}
func (s256 *Sha256Verifier) String() string {
return fmt.Sprintf("sha256:%x", s256.Expected)
}