-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
39 lines (36 loc) · 826 Bytes
/
key.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
package nuts
// KeyLen returns the minimum number of bytes required to represent x; the result is 1 for x == 0.
// Returns 1-8.
func KeyLen(x uint64) int {
n := 1
if x >= 1<<32 {
x >>= 32
n += 4
}
if x >= 1<<16 {
x >>= 16
n += 2
}
if x >= 1<<8 {
x >>= 8
n += 1
}
return n
}
// Key is a byte slice with methods for serializing uint64 (big endian).
// Length can minimized (<8) with KeyLen.
// make(Key, KeyLen(uint64(max)))
// Large Keys can constructed by slicing.
// uuid := make(Key, 16)
// uuid[:8].Put(a)
// uuid[8:].Put(b)
type Key []byte
// Put serializes x into the buffer (big endian). Behavior is undefined when x
// does not fit, so the caller must ensure c is large enough.
func (c Key) Put(x uint64) {
s := uint(8 * (len(c) - 1))
for i := range c {
c[i] = byte(x >> s)
s -= 8
}
}