aboutsummaryrefslogtreecommitdiff
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
parent0ced3ecc87f19aa35f9e6919ca822ac71f57b20a (diff)
..
-rw-r--r--zsig/header.go33
-rw-r--r--zsig/sum.go41
2 files changed, 33 insertions, 41 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
+}
diff --git a/zsig/sum.go b/zsig/sum.go
deleted file mode 100644
index 40bf24c..0000000
--- a/zsig/sum.go
+++ /dev/null
@@ -1,41 +0,0 @@
-package zsig
-
-import (
- "bytes"
- "crypto/sha512"
- "errors"
- "io"
- "log"
-)
-
-const (
- Alg = "SHA512/256"
- BlockSize = 65536
-)
-
-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 {
- log.Println(blockCount)
- return errors.New("len mismatch")
- }
- return nil
-}