From ece00da8dcd3e9b6976e59a6d0ee92eca6a5db2c Mon Sep 17 00:00:00 2001 From: dtluna Date: Wed, 27 Mar 2019 14:19:19 +0300 Subject: [PATCH] Add sha1sum --- README.md | 2 +- hashsum.go | 19 ++++++++++++++----- sha1sum/sha1sum.go | 9 +++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 sha1sum/sha1sum.go diff --git a/README.md b/README.md index 4137675..a979d16 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Utilities: * [ ] `sed` * [ ] `seq` * [ ] `setsid` - * [ ] `sha1sum` + * [x] `sha1sum` * [ ] `sha224sum` * [ ] `sha256sum` * [ ] `sha384sum` diff --git a/hashsum.go b/hashsum.go index 341c571..e15ecc2 100644 --- a/hashsum.go +++ b/hashsum.go @@ -3,6 +3,7 @@ package coreutils import ( "bufio" "crypto/md5" + "crypto/sha1" "errors" "fmt" "hash" @@ -14,24 +15,32 @@ import ( ) var MD5Regex = regexp.MustCompile("^(?P[0-9a-f]{32}) (?P.*)$") +var SHA1Regex = regexp.MustCompile("^(?P[0-9a-f]{40}) (?P.*)$") -func MD5Sum(r io.Reader) (hash.Hash, error) { - h := md5.New() +// SumFunc is a type of function that computes a hash.Hash for data in io.Reader +type SumFunc func(io.Reader) (hash.Hash, error) + +func copyIntoHash(r io.Reader, h hash.Hash) (hash.Hash, error) { if _, err := io.Copy(h, r); err != nil { return nil, err } return h, nil } +func MD5Sum(r io.Reader) (hash.Hash, error) { + return copyIntoHash(r, md5.New()) +} + +func SHA1Sum(r io.Reader) (hash.Hash, error) { + return copyIntoHash(r, sha1.New()) +} + type CheckingResults struct { ImproperlyFormattedCount uint InvalidChecksumCount uint FilesNotRead uint } -// SumFunc is a type of function that computes a hash.Hash for data in io.Reader -type SumFunc func(io.Reader) (hash.Hash, error) - // ImproperlyFormattedErr is an error return when a line for checking // has an incorrect number or set of characters var ImproperlyFormattedErr = errors.New("improperly formatted line") diff --git a/sha1sum/sha1sum.go b/sha1sum/sha1sum.go new file mode 100644 index 0000000..d12d2c1 --- /dev/null +++ b/sha1sum/sha1sum.go @@ -0,0 +1,9 @@ +package main + +import ( + common "source.heropunch.io/tomo/go-coreutils" +) + +func main() { + common.SumMain(common.SHA1Regex, common.SHA1Sum) +}