mirror of
http://git.dtluna.net/tomo/go-coreutils.git
synced 2025-01-07 06:09:52 +00:00
Add sha1sum
This commit is contained in:
parent
7d6c06b251
commit
ece00da8dc
@ -67,7 +67,7 @@ Utilities:
|
||||
* [ ] `sed`
|
||||
* [ ] `seq`
|
||||
* [ ] `setsid`
|
||||
* [ ] `sha1sum`
|
||||
* [x] `sha1sum`
|
||||
* [ ] `sha224sum`
|
||||
* [ ] `sha256sum`
|
||||
* [ ] `sha384sum`
|
||||
|
19
hashsum.go
19
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<hash>[0-9a-f]{32}) (?P<filename>.*)$")
|
||||
var SHA1Regex = regexp.MustCompile("^(?P<hash>[0-9a-f]{40}) (?P<filename>.*)$")
|
||||
|
||||
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")
|
||||
|
9
sha1sum/sha1sum.go
Normal file
9
sha1sum/sha1sum.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
common "source.heropunch.io/tomo/go-coreutils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
common.SumMain(common.SHA1Regex, common.SHA1Sum)
|
||||
}
|
Loading…
Reference in New Issue
Block a user