forked from TrueCloudLab/restic
Generalize hash in backend
This commit is contained in:
parent
5f0cce8546
commit
842dea173c
2 changed files with 11 additions and 5 deletions
|
@ -19,6 +19,13 @@ var (
|
||||||
ErrMultipleIDMatches = errors.New("multiple IDs with prefix found")
|
ErrMultipleIDMatches = errors.New("multiple IDs with prefix found")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
newHash = sha256.New
|
||||||
|
hashData = sha256.Sum256
|
||||||
|
)
|
||||||
|
|
||||||
|
const hashSize = sha256.Size
|
||||||
|
|
||||||
// Each lists all entries of type t in the backend and calls function f() with
|
// Each lists all entries of type t in the backend and calls function f() with
|
||||||
// the id and data.
|
// the id and data.
|
||||||
func Each(be interface {
|
func Each(be interface {
|
||||||
|
@ -92,7 +99,7 @@ func Uncompress(data []byte) []byte {
|
||||||
|
|
||||||
// Hash returns the ID for data.
|
// Hash returns the ID for data.
|
||||||
func Hash(data []byte) ID {
|
func Hash(data []byte) ID {
|
||||||
h := sha256.Sum256(data)
|
h := hashData(data)
|
||||||
id := idPool.Get().(ID)
|
id := idPool.Get().(ID)
|
||||||
copy(id, h[:])
|
copy(id, h[:])
|
||||||
return id
|
return id
|
||||||
|
|
|
@ -2,7 +2,6 @@ package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -10,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// IDSize contains the size of an ID, in bytes.
|
// IDSize contains the size of an ID, in bytes.
|
||||||
const IDSize = sha256.Size
|
const IDSize = hashSize
|
||||||
|
|
||||||
// References content within a repository.
|
// References content within a repository.
|
||||||
type ID []byte
|
type ID []byte
|
||||||
|
@ -26,7 +25,7 @@ func ParseID(s string) (ID, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(b) != IDSize {
|
if len(b) != IDSize {
|
||||||
return nil, errors.New("invalid length for sha256 hash")
|
return nil, errors.New("invalid length for hash")
|
||||||
}
|
}
|
||||||
|
|
||||||
return ID(b), nil
|
return ID(b), nil
|
||||||
|
@ -83,7 +82,7 @@ func (id *ID) UnmarshalJSON(b []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func IDFromData(d []byte) ID {
|
func IDFromData(d []byte) ID {
|
||||||
hash := sha256.Sum256(d)
|
hash := hashData(d)
|
||||||
id := idPool.Get().(ID)
|
id := idPool.Get().(ID)
|
||||||
copy(id, hash[:])
|
copy(id, hash[:])
|
||||||
return id
|
return id
|
||||||
|
|
Loading…
Reference in a new issue