diff --git a/backend/generic.go b/backend/generic.go index d8c72b9e0..c528f8998 100644 --- a/backend/generic.go +++ b/backend/generic.go @@ -1,29 +1,14 @@ package backend -import ( - "crypto/sha256" - "errors" -) +import "errors" -const ( - MinPrefixLength = 8 -) +// ErrNoIDPrefixFound is returned by Find() when no ID for the given prefix +// could be found. +var ErrNoIDPrefixFound = errors.New("no ID found") -var ( - ErrNoIDPrefixFound = errors.New("no ID found") - ErrMultipleIDMatches = errors.New("multiple IDs with prefix found") -) - -var ( - hashData = sha256.Sum256 -) - -const hashSize = sha256.Size - -// Hash returns the ID for data. -func Hash(data []byte) ID { - return hashData(data) -} +// ErrMultipleIDMatches is returned by Find() when multiple IDs with the given +// prefix are found. +var ErrMultipleIDMatches = errors.New("multiple IDs with prefix found") // Find loads the list of all blobs of type t and searches for names which // start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. @@ -52,6 +37,8 @@ func Find(be Lister, t Type, prefix string) (string, error) { return "", ErrNoIDPrefixFound } +const minPrefixLength = 8 + // PrefixLength returns the number of bytes required so that all prefixes of // all names of type t are unique. func PrefixLength(be Lister, t Type) (int, error) { @@ -66,7 +53,7 @@ func PrefixLength(be Lister, t Type) (int, error) { // select prefixes of length l, test if the last one is the same as the current one outer: - for l := MinPrefixLength; l < IDSize; l++ { + for l := minPrefixLength; l < IDSize; l++ { var last string for _, name := range list { diff --git a/backend/id.go b/backend/id.go index 966cd7a4e..e76d264f5 100644 --- a/backend/id.go +++ b/backend/id.go @@ -2,13 +2,19 @@ package backend import ( "bytes" + "crypto/sha256" "encoding/hex" "encoding/json" "errors" ) +// Hash returns the ID for data. +func Hash(data []byte) ID { + return sha256.Sum256(data) +} + // IDSize contains the size of an ID, in bytes. -const IDSize = hashSize +const IDSize = sha256.Size // ID references content within a repository. type ID [IDSize]byte @@ -98,7 +104,3 @@ func (id *ID) UnmarshalJSON(b []byte) error { return nil } - -func IDFromData(d []byte) ID { - return hashData(d) -}