-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
62 lines (57 loc) · 1.43 KB
/
generate.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
// Copyright (c) 2023 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package refid
import (
"crypto/rand"
"fmt"
"io"
"time"
)
func generate(t Type) ([]byte, error) {
b := make([]byte, size)
var err error
switch t {
case TimePrefixed:
err = setRandom(b[typeIndex-1:], rand.Reader)
setTime(b, time.Now().UTC().UnixMilli())
// set type to 0 (clear lowest bit)
b[typeIndex] &^= 0x01
case RandomPrefixed:
err = setRandom(b, rand.Reader)
// set type to 1 (set bit 1)
b[typeIndex] |= 0x01
default:
return b, fmt.Errorf("unknown type specified")
}
// clear tag
b[tagIndex] = 0x00
return b, err
}
func setTime(b []byte, millis int64) {
// A 45 bit timestamp of milliseconds since epoch.
// Which should be fine until around year 3084
// 1-7 bytes: big-endian unsigned number of Unix epoch timestamp
if millis < 0 {
millis = 0
} else if millis > maxTime {
millis = maxTime
}
ms := uint64(millis) << 3 // #nosec G115 - guarded above and in callers
b[0] = byte(ms >> 40)
b[1] = byte(ms >> 32)
b[2] = byte(ms >> 24)
b[3] = byte(ms >> 16)
b[4] = byte(ms >> 8)
// clear all but the bottom 3 bits of b[5],
// as that is random data we want to leave as radom
b[5] = byte(ms) | (b[5] & 0b111)
}
// use cyrpto/rand for non-test code
func setRandom(b []byte, randR io.Reader) error {
_, err := io.ReadFull(randR, b)
if err != nil {
return err
}
return nil
}