|
|
@ -3,6 +3,7 @@ package coreutils |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bufio" |
|
|
|
"bufio" |
|
|
|
"crypto/md5" |
|
|
|
"crypto/md5" |
|
|
|
|
|
|
|
"crypto/sha1" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"hash" |
|
|
|
"hash" |
|
|
@ -14,24 +15,32 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var MD5Regex = regexp.MustCompile("^(?P<hash>[0-9a-f]{32}) (?P<filename>.*)$") |
|
|
|
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) { |
|
|
|
// SumFunc is a type of function that computes a hash.Hash for data in io.Reader
|
|
|
|
h := md5.New() |
|
|
|
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 { |
|
|
|
if _, err := io.Copy(h, r); err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
return h, nil |
|
|
|
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 { |
|
|
|
type CheckingResults struct { |
|
|
|
ImproperlyFormattedCount uint |
|
|
|
ImproperlyFormattedCount uint |
|
|
|
InvalidChecksumCount uint |
|
|
|
InvalidChecksumCount uint |
|
|
|
FilesNotRead 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
|
|
|
|
// ImproperlyFormattedErr is an error return when a line for checking
|
|
|
|
// has an incorrect number or set of characters
|
|
|
|
// has an incorrect number or set of characters
|
|
|
|
var ImproperlyFormattedErr = errors.New("improperly formatted line") |
|
|
|
var ImproperlyFormattedErr = errors.New("improperly formatted line") |
|
|
|