...
1 package data
2
3 import (
4 "crypto/sha256"
5 "encoding/hex"
6 "errors"
7 )
8
9 type HexBytes []byte
10
11 func (b *HexBytes) UnmarshalJSON(data []byte) error {
12 if len(data) < 2 || len(data)%2 != 0 || data[0] != '"' || data[len(data)-1] != '"' {
13 return errors.New("tuf: invalid JSON hex bytes")
14 }
15 res := make([]byte, hex.DecodedLen(len(data)-2))
16 _, err := hex.Decode(res, data[1:len(data)-1])
17 if err != nil {
18 return err
19 }
20 *b = res
21 return nil
22 }
23 func (b *HexBytes) FromString(data []byte) error {
24 res := make([]byte, hex.DecodedLen(len(data)))
25 _, err := hex.Decode(res, data)
26 if err != nil {
27 return err
28 }
29 *b = res
30 return nil
31 }
32
33 func (b HexBytes) MarshalJSON() ([]byte, error) {
34 res := make([]byte, hex.EncodedLen(len(b))+2)
35 res[0] = '"'
36 res[len(res)-1] = '"'
37 hex.Encode(res[1:], b)
38 return res, nil
39 }
40
41 func (b HexBytes) String() string {
42 return hex.EncodeToString(b)
43 }
44
45
46
47
48 func PathHexDigest(s string) string {
49 b := sha256.Sum256([]byte(s))
50 return hex.EncodeToString(b[:])
51 }
52
View as plain text