aboutsummaryrefslogtreecommitdiff
path: root/chksum
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-05-03 13:27:19 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-05-03 13:27:19 +0200
commit2cd8e0e842b667f91babb25d0901cfd68d55ccb2 (patch)
tree24430646c4121542b2b3c150ae4ab4b1e70afd93 /chksum
parentc8fc9a36594a62d0ea335d6785914925c8d9c00c (diff)
errors, reader
Diffstat (limited to 'chksum')
-rw-r--r--chksum/chksum.go31
1 files changed, 20 insertions, 11 deletions
diff --git a/chksum/chksum.go b/chksum/chksum.go
index 8a5f9d3..b659be0 100644
--- a/chksum/chksum.go
+++ b/chksum/chksum.go
@@ -15,6 +15,12 @@ import (
"regexp"
)
+var (
+ ErrChecksum = errors.New("invalid checksum")
+ ErrHashAlg = errors.New("unknown hash algorithm")
+ ErrParse = errors.New("invalid hash entry")
+)
+
/* checksum file format
SHA512 (filename) = hex-encoded checksum
or
@@ -43,28 +49,31 @@ func ParseFile(fname string) (Checklist, error) {
return nil, err
}
defer fd.Close()
+ return Parse(fd)
+}
+func Parse(r io.Reader) (Checklist, error) {
var checklist Checklist
re := regexp.MustCompile(`(\w+) \(([^)]+)\) = (\w+)`)
- scanner := bufio.NewScanner(fd)
+ scanner := bufio.NewScanner(r)
for scanner.Scan() {
- r := re.FindStringSubmatch(scanner.Text())
- if len(r) != 4 {
- return nil, errors.New("invalid content")
+ match := re.FindStringSubmatch(scanner.Text())
+ if len(match) != 4 {
+ return nil, ErrParse
}
- h, ok := hashes[r[1]]
+ hash, ok := hashes[match[1]]
if !ok {
- return nil, errors.New("unknown hash alg")
+ return nil, ErrHashAlg
}
- bytes, err := hex.DecodeString(r[3])
+ bytes, err := hex.DecodeString(match[3])
if err != nil {
return nil, err
}
cs := Checksum{
- FileName: r[2],
- Alg: r[1],
+ FileName: match[2],
+ Alg: match[1],
Bytes: bytes,
- Hash: h(),
+ Hash: hash(),
}
checklist = append(checklist, cs)
}
@@ -79,7 +88,7 @@ func (c Checksum) Check() error {
defer fd.Close()
io.Copy(c.Hash, fd)
if !bytes.Equal(c.Hash.Sum(nil), c.Bytes) {
- return errors.New("hash mismatch")
+ return ErrChecksum
}
return nil
}