-
Notifications
You must be signed in to change notification settings - Fork 4
/
idgen.go
57 lines (45 loc) · 1005 Bytes
/
idgen.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
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package idgen
import (
"crypto/rand"
"encoding/hex"
"io"
"strconv"
)
type randReader struct{}
func (randReader) Read(p []byte) (int, error) {
return rand.Read(p)
}
type IDGen interface {
Generate() string
}
type idGen struct {
reader io.Reader
length int
}
// NewIDGen creates a new IDGen. The length parameter is the desired length of the generated id.
func NewIDGen(r io.Reader, length int) IDGen {
return &idGen{
reader: r,
length: length,
}
}
func (g *idGen) Generate() string {
data := make([]byte, (g.length/2)+1)
for {
_, _ = g.reader.Read(data)
id := hex.EncodeToString(data)
// Truncated versions of the id should not be numerical.
if _, err := strconv.ParseInt(id[:12], 10, 64); err != nil {
continue
}
return id[:g.length]
}
}
var (
Default = NewIDGen(randReader{}, DefaultIDLength)
)
const (
DefaultIDLength = 63
)