forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
34 lines (28 loc) · 812 Bytes
/
types.go
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
package desync
import (
"encoding/hex"
"github.com/pkg/errors"
)
// ChunkID is the SHA512/256 in binary encoding
type ChunkID [32]byte
// ChunkIDFromSlice converts a SHA512/256 encoded as byte slice into a ChunkID.
// It's expected the slice is of the correct length
func ChunkIDFromSlice(b []byte) (ChunkID, error) {
var c ChunkID
if len(b) != len(c) {
return c, errors.New("chunk id string not of right size")
}
copy(c[:], b)
return c, nil
}
// ChunkIDFromString converts a SHA512/56 encoded as string into a ChunkID
func ChunkIDFromString(id string) (ChunkID, error) {
b, err := hex.DecodeString(id)
if err != nil {
return ChunkID{}, errors.Wrap(err, "failed to decode chunk id string")
}
return ChunkIDFromSlice(b)
}
func (c ChunkID) String() string {
return hex.EncodeToString(c[:])
}