From 6b387241e474194ad030b500903c030ba70cc63b Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 26 Jul 2017 16:54:13 +0200 Subject: crypto --- chksum/chksum.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'chksum/chksum.go') 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) } -- cgit v1.2.3