-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifier.go
41 lines (32 loc) · 876 Bytes
/
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
package selfupdate
import (
"bytes"
"context"
"errors"
"io"
"selfupdate.blockthrough.com/pkg/crypto"
"selfupdate.blockthrough.com/pkg/hash"
)
var (
ErrVerificationFailed = errors.New("verification failed")
)
func NewHashVerifier(publicKey crypto.PublicKey) Verifier {
return VerifierFunc(func(ctx context.Context, r io.Reader) io.Reader {
var signedHash [hash.HashSize + crypto.Overhead]byte
if _, err := io.ReadFull(r, signedHash[:]); err != nil {
return newErrorReader(err)
}
var buffer bytes.Buffer
contentHash, err := hash.FromReader(io.TeeReader(r, &buffer))
if err != nil {
return newErrorReader(err)
}
if !publicKey.Verify(signedHash[:]) {
return newErrorReader(ErrVerificationFailed)
}
if !bytes.Equal(contentHash, signedHash[crypto.Overhead:]) {
return newErrorReader(ErrVerificationFailed)
}
return &buffer
})
}