1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package gitinterface
import (
"bytes"
"crypto/sha1" //nolint:gosec
"crypto/sha256"
"encoding/hex"
"errors"
)
const (
GitBlobHashName = "gitBlob"
SHA256HashName = "sha256"
)
var (
zeroSHA1HashBytes = [sha1.Size]byte{}
zeroSHA256HashBytes = [sha256.Size]byte{}
)
var (
ErrInvalidHashEncoding = errors.New("hash string is not hex encoded")
ErrInvalidHashLength = errors.New("hash string is wrong length")
)
// Hash represents a Git object hash. It is a lightweight wrapper around the
// standard hex encoded representation of a SHA-1 or SHA-256 hash used by Git.
type Hash []byte
// String returns the hex encoded hash.
func (h Hash) String() string {
return hex.EncodeToString(h[:])
}
// IsZero compares the hash to see if it's the zero hash for either SHA-1 or
// SHA-256.
func (h Hash) IsZero() bool {
return bytes.Equal(h[:], zeroSHA1HashBytes[:]) || bytes.Equal(h[:], zeroSHA256HashBytes[:])
}
// Equal compares the hash to another provided Hash to see if they're equal.
func (h Hash) Equal(other Hash) bool {
return bytes.Equal(h[:], other[:])
}
// ZeroHash represents an empty Hash.
// TODO: use SHA-256 zero hash for repositories that have that as the default.
var ZeroHash = Hash(zeroSHA1HashBytes[:])
// NewHash returns a Hash object after ensuring the input string is correctly
// encoded.
func NewHash(h string) (Hash, error) {
if len(h) != (sha1.Size*2) && len(h) != (sha256.Size*2) {
return ZeroHash, ErrInvalidHashLength
}
hash, err := hex.DecodeString(h)
if err != nil {
return ZeroHash, ErrInvalidHashEncoding
}
return Hash(hash), nil
}
|