-
Notifications
You must be signed in to change notification settings - Fork 32
/
identity.go
155 lines (127 loc) · 3.34 KB
/
identity.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Package identity defines the ID format used for uniquely identifying
// objects in Torus.
package identity
import (
"crypto/rand"
"encoding/base32"
"encoding/json"
"errors"
"strconv"
"strings"
"github.com/dchest/blake2b"
)
const (
base32Alphabet = "0123456789abcdefghjkmnpqrtuvwxyz"
idVersion = 0x01
byteLength = 18
)
// Identifiable is the interface implemented by objects that can be given
// IDs.
type Identifiable interface {
Version() int
Type() byte
}
// Immutable structs are Identifiables that do not change, and should be signed.
type Immutable interface {
Identifiable
Immutable() // We don't ever need to call this, its just for type checking.
}
// Mutable structs are Identifiables that can be changed.
type Mutable interface {
Identifiable
Mutable() // also just for type checking.
}
var lowerBase32 = base32.NewEncoding(base32Alphabet)
// ID is an encoded unique identifier for an object.
//
// The first byte holds the schema version of the id itself.
// The second byte holds the type of the object.
// The remaining 16 bytes hold a digest of the contents of the object for
// immutable objects, or a random value for mutable objects.
type ID [18]byte
// NewMutable returns a new ID for a mutable object.
func NewMutable(body Mutable) (ID, error) {
id := ID{idVersion, body.Type()}
_, err := rand.Read(id[2:])
if err != nil {
return ID{}, err
}
return id, nil
}
// NewImmutable returns a new signed ID for an immutable object.
//
// sig should be a registry.Signature type
func NewImmutable(body Immutable, sig interface{}) (ID, error) {
h, err := blake2b.New(&blake2b.Config{Size: 16})
if err != nil {
return ID{}, err
}
h.Write([]byte(strconv.Itoa(body.Version())))
b, err := json.Marshal(body)
if err != nil {
return ID{}, err
}
h.Write(b)
b, err = json.Marshal(sig)
if err != nil {
return ID{}, err
}
h.Write(b)
id := ID{idVersion, body.Type()}
copy(id[2:], h.Sum(nil))
return id, nil
}
// DecodeFromString returns an ID that is stored in the given string.
func DecodeFromString(value string) (ID, error) {
buf, err := decodeFromByte([]byte(value))
if err != nil {
return ID{}, err
}
id := ID{}
copy(id[:], buf)
return id, nil
}
// Type returns the binary encoded object type represented by this ID.
func (id *ID) Type() byte {
return id[1]
}
func (id *ID) String() string {
return strings.TrimRight(lowerBase32.EncodeToString(id[:]), "=")
}
// MarshalJSON implements the json.Marshaler interface for IDs.
//
// IDs are encoded in unpadded base32.
func (id *ID) MarshalJSON() ([]byte, error) {
return []byte("\"" + id.String() + "\""), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface for IDs.
func (id *ID) UnmarshalJSON(b []byte) error {
if len(b) < 2 || b[0] != byte('"') || b[len(b)-1] != byte('"') {
return errors.New("value is not a string")
}
return id.fillID(b[1 : len(b)-1])
}
func (id *ID) fillID(raw []byte) error {
out, err := decodeFromByte(raw)
if err != nil {
return err
}
copy(id[:], out)
return nil
}
func decodeFromByte(raw []byte) ([]byte, error) {
pad := 8 - (len(raw) % 8)
nb := make([]byte, len(raw)+pad)
copy(nb, raw)
for i := 0; i < pad; i++ {
nb[len(raw)+i] = '='
}
out, err := lowerBase32.DecodeString(string(nb))
if err != nil {
return nil, err
}
if len(out) != 18 {
return nil, errors.New("Incorrect length for id")
}
return out, nil
}