Documentation ¶
Overview ¶
Package hashcs provides functions to calculate the hash checksum of one local file.
Index ¶
Constants ¶
const NumHash int = 18
NumHash is the number of supported hash algorithms.
Variables ¶
var Hashes = [NumHash]crypto.Hash{ crypto.MD4, crypto.MD5, crypto.SHA1, crypto.SHA224, crypto.SHA256, crypto.SHA384, crypto.SHA512, crypto.SHA512_224, crypto.SHA512_256, crypto.RIPEMD160, crypto.SHA3_224, crypto.SHA3_256, crypto.SHA3_384, crypto.SHA3_512, crypto.BLAKE2s_256, crypto.BLAKE2b_256, crypto.BLAKE2b_384, crypto.BLAKE2b_512, }
Hashes are the supported hash algorithms.
All its items are available (i.e., have been linked to the binary).
var Names = [NumHash][]string{
{"md4"},
{"md5", "m"},
{"sha-1", "sha_1", "sha1"},
{"sha-224", "sha_224", "sha224"},
{"sha-256", "sha_256", "sha256", "s"},
{"sha-384", "sha_384", "sha384"},
{"sha-512", "sha_512", "sha512"},
{
"sha-512/224", "sha-512_224", "sha-512224",
"sha_512/224", "sha_512_224", "sha_512224",
"sha512/224", "sha512_224", "sha512224",
},
{
"sha-512/256", "sha-512_256", "sha-512256",
"sha_512/256", "sha_512_256", "sha_512256",
"sha512/256", "sha512_256", "sha512256",
},
{"ripemd-160", "ripemd_160", "ripemd160"},
{"sha3-224", "sha3_224", "sha3224"},
{"sha3-256", "sha3_256", "sha3256"},
{"sha3-384", "sha3_384", "sha3384"},
{"sha3-512", "sha3_512", "sha3512"},
{"blake2s-256", "blake2s_256", "blake2s256"},
{"blake2b-256", "blake2b_256", "blake2b256"},
{"blake2b-384", "blake2b_384", "blake2b384"},
{"blake2b-512", "blake2b_512", "blake2b512"},
}
Names are the names and aliases of the supported hash algorithms.
Each item (of type []string) starts with the hash algorithm name, followed by its aliases. The hash algorithm name is the lowercase of the name returned by the method String of the corresponding crypto.Hash.
Functions ¶
This section is empty.
Types ¶
type HashChecksum ¶
type HashChecksum struct { // HashName is the name of the hash algorithm. // // HashName is not guaranteed in Names and // should only be used for display. HashName string `json:"hashName"` // Checksum is the hexadecimal representation of the hash checksum. Checksum string `json:"checksum"` }
HashChecksum consists of the hash algorithm name and the hexadecimal representation of the checksum.
func CalculateChecksum ¶
func CalculateChecksum(filename string, upper bool, hashNames []string) ( checksums []HashChecksum, err error)
CalculateChecksum calculates the hash checksum of the specified file.
If the file is a directory, CalculateChecksum reports github.com/donyori/gogo/filesys.ErrIsDir and returns nil checksums. (To test whether err is github.com/donyori/gogo/filesys.ErrIsDir, use function errors.Is.)
upper indicates whether to use uppercase in hexadecimal representation.
hashNames are the names (or aliases) of the hash algorithms. Each name must be in the list Names. Otherwise, CalculateChecksum reports a *UnknownHashAlgorithmError. (To test whether err is *UnknownHashAlgorithmError, use function errors.As.) Duplicate algorithms are ignored. (For example, if the argument hashNames is []string{"sha-256", "sha256", "s"}, the returned checksums contain only one item corresponding to the hash algorithm SHA-256.) If there are no items in hashNames, CalculateChecksum calculates the SHA-256 checksum.
The returned checksums are sorted in the order of their names displayed in Names.
For each item in the returned checksums, the field HashName is the name returned by the method String of the corresponding crypto.Hash.
type UnknownHashAlgorithmError ¶
type UnknownHashAlgorithmError struct {
// contains filtered or unexported fields
}
UnknownHashAlgorithmError is an error indicating that the specified hash algorithm is unknown.
func NewUnknownHashAlgorithmError ¶
func NewUnknownHashAlgorithmError(hashName string) *UnknownHashAlgorithmError
NewUnknownHashAlgorithmError creates a new UnknownHashAlgorithmError with the specified hash algorithm name.
func (*UnknownHashAlgorithmError) Error ¶
func (e *UnknownHashAlgorithmError) Error() string
Error returns the error message.
If e is nil, it returns "<nil *UnknownHashAlgorithmError>".
func (*UnknownHashAlgorithmError) HashName ¶
func (e *UnknownHashAlgorithmError) HashName() string
HashName returns the hash algorithm name recorded in e.
If e is nil, it returns "<nil>".