aboutsummaryrefslogtreecommitdiff
path: root/zsig/header.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-07-26 02:48:44 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-07-26 02:48:44 +0200
commit0dde4c607250fa745c7bd84298a1ebeb7ad63430 (patch)
tree908209008e8415d2a005453c7319183b79f6e526 /zsig/header.go
parent0ced3ecc87f19aa35f9e6919ca822ac71f57b20a (diff)
..
Diffstat (limited to 'zsig/header.go')
-rw-r--r--zsig/header.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/zsig/header.go b/zsig/header.go
index 46f29ae..20d4db7 100644
--- a/zsig/header.go
+++ b/zsig/header.go
@@ -3,7 +3,9 @@ package zsig
import (
"bufio"
"bytes"
+ "crypto/sha512"
"encoding/hex"
+ "errors"
"fmt"
"io"
"strconv"
@@ -11,6 +13,11 @@ import (
"time"
)
+const (
+ Alg = "SHA512/256"
+ BlockSize = 65536
+)
+
type ZHeader struct {
Date time.Time
KeyFile string
@@ -70,3 +77,29 @@ func Parse(r io.Reader) (ZHeader, error) {
}
return h, s.Err()
}
+
+func (z ZHeader) Verify(r io.Reader) error {
+ if z.Alg != Alg {
+ return errors.New("invalid hash algorithm")
+ }
+ h := sha512.New512_256() // from z.Alg
+ blockCount := len(z.Sums)
+ for _, sum := range z.Sums {
+ h.Reset()
+ n, err := io.CopyN(h, r, z.BlockSize)
+ if n == 0 && err == io.EOF {
+ break
+ }
+ if err != nil && err != io.EOF {
+ return err
+ }
+ if !bytes.Equal(sum, h.Sum(nil)) {
+ return errors.New("sum mismatch")
+ }
+ blockCount--
+ }
+ if blockCount != 0 {
+ return errors.New("count mismatch")
+ }
+ return nil
+}