aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-07-26 16:54:13 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-07-26 16:54:13 +0200
commit6b387241e474194ad030b500903c030ba70cc63b (patch)
treec8286eed15bc7d63604e930cc0203c4ce2222cf2
parentbed40dec41ca103af59335f050fd0be9b611c34f (diff)
crypto
-rw-r--r--chksum/chksum.go34
-rw-r--r--chksum/chksum_test.go1
-rw-r--r--chksum/testdata/1kbin1024 -> 0 bytes
-rw-r--r--chksum/testdata/SIZE2
4 files changed, 16 insertions, 21 deletions
diff --git a/chksum/chksum.go b/chksum/chksum.go
index 589c804..84b72b9 100644
--- a/chksum/chksum.go
+++ b/chksum/chksum.go
@@ -4,10 +4,7 @@ package chksum
import (
"bufio"
"bytes"
- "crypto/md5"
- "crypto/sha1"
- "crypto/sha256"
- "crypto/sha512"
+ "crypto"
"encoding/hex"
"errors"
"hash"
@@ -16,6 +13,12 @@ import (
"os"
"path"
"regexp"
+
+ // crypto.RegisterHash
+ _ "crypto/md5"
+ _ "crypto/sha1"
+ _ "crypto/sha256"
+ _ "crypto/sha512"
)
var (
@@ -38,18 +41,13 @@ type Checksum struct {
type Checklist []Checksum
-type hashDef struct {
- newHash func() hash.Hash
- decode func(string) ([]byte, error)
-}
-
-var hashes = map[string]hashDef{
- "SHA512/256": {sha512.New512_256, hex.DecodeString},
- "SHA512": {sha512.New, hex.DecodeString},
- "SHA256": {sha256.New, hex.DecodeString},
- "SHA1": {sha1.New, hex.DecodeString},
- "MD5": {md5.New, hex.DecodeString},
- "SIZE": {NewSize, ParseSize},
+// known hashes
+var hashes = map[string]crypto.Hash{
+ "MD5": crypto.MD5,
+ "SHA1": crypto.SHA1,
+ "SHA256": crypto.SHA256,
+ "SHA512": crypto.SHA512,
+ "SHA512/256": crypto.SHA512_256,
}
func ParseFile(fname string) (Checklist, error) {
@@ -81,14 +79,14 @@ func parse(r io.Reader) (Checklist, error) {
log.Println(match)
return nil, ErrHashAlg
}
- bytes, err := hash.decode(match[3])
+ bytes, err := hex.DecodeString(match[3])
if err != nil {
return nil, err
}
cs := Checksum{
FileName: path.Clean(match[2]),
Bytes: bytes,
- Hash: hash.newHash(),
+ Hash: hash.New(),
}
checklist = append(checklist, cs)
}
diff --git a/chksum/chksum_test.go b/chksum/chksum_test.go
index e799738..d51a5ae 100644
--- a/chksum/chksum_test.go
+++ b/chksum/chksum_test.go
@@ -12,7 +12,6 @@ func TestParseFile(t *testing.T) {
{"testdata/SHA512"},
{"testdata/SHA512256"},
{"testdata/mixed"},
- {"testdata/SIZE"},
}
for _, tc := range testCases {
t.Run(tc.file, func(t *testing.T) {
diff --git a/chksum/testdata/1k b/chksum/testdata/1k
deleted file mode 100644
index 06d7405..0000000
--- a/chksum/testdata/1k
+++ /dev/null
Binary files differ
diff --git a/chksum/testdata/SIZE b/chksum/testdata/SIZE
deleted file mode 100644
index 06c14d0..0000000
--- a/chksum/testdata/SIZE
+++ /dev/null
@@ -1,2 +0,0 @@
-SIZE (testdata/empty) = 0
-SIZE (testdata/1k) = 1024