-
Notifications
You must be signed in to change notification settings - Fork 12
/
id.go
150 lines (120 loc) · 2.63 KB
/
id.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
// Copyright 2019 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package idutil
import (
"crypto/rand"
"errors"
"net"
"github.com/sony/sonyflake"
hashids "github.com/speps/go-hashids"
"openpitrix.io/notification/pkg/util/stringutil"
)
var sf *sonyflake.Sonyflake
var upperMachineID uint16
func init() {
var st sonyflake.Settings
sf = sonyflake.NewSonyflake(st)
if sf == nil {
sf = sonyflake.NewSonyflake(sonyflake.Settings{
MachineID: lower16BitIP,
})
upperMachineID, _ = upper16BitIP()
}
}
func lower16BitIP() (uint16, error) {
ip, err := IPv4()
if err != nil {
return 0, err
}
return uint16(ip[2])<<8 + uint16(ip[3]), nil
}
func upper16BitIP() (uint16, error) {
ip, err := IPv4()
if err != nil {
return 0, err
}
return uint16(ip[0])<<8 + uint16(ip[1]), nil
}
func IPv4() (net.IP, error) {
as, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, a := range as {
ipnet, ok := a.(*net.IPNet)
if !ok || ipnet.IP.IsLoopback() {
continue
}
ip := ipnet.IP.To4()
return ip, nil
}
return nil, errors.New("no ip address")
}
func GetIntId() uint64 {
id, err := sf.NextID()
if err != nil {
panic(err)
}
return id
}
// format likes: B6BZVN3mOPvx
func GetUuid(prefix string) string {
id := GetIntId()
hd := hashids.NewData()
h, err := hashids.NewWithData(hd)
if err != nil {
panic(err)
}
i, err := h.Encode([]int{int(id)})
if err != nil {
panic(err)
}
return prefix + stringutil.Reverse(i)
}
const (
Alphabet62 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
Alphabet36 = "abcdefghijklmnopqrstuvwxyz1234567890"
)
// format likes: 300m50zn91nwz5
func GetUuid36(prefix string) string {
id := GetIntId()
hd := hashids.NewData()
hd.Alphabet = Alphabet36
h, err := hashids.NewWithData(hd)
if err != nil {
panic(err)
}
i, err := h.Encode([]int{int(id)})
if err != nil {
panic(err)
}
return prefix + stringutil.Reverse(i)
}
func randString(letters string, n int) string {
output := make([]byte, n)
// We will take n bytes, one byte for each character of output.
randomness := make([]byte, n)
// read all random
_, err := rand.Read(randomness)
if err != nil {
panic(err)
}
l := len(letters)
// fill output
for pos := range output {
// get random item
random := uint8(randomness[pos])
// random % 64
randomPos := random % uint8(l)
// put into output
output[pos] = letters[randomPos]
}
return string(output)
}
func GetSecret() string {
return randString(Alphabet62, 50)
}
func GetRefreshToken() string {
return randString(Alphabet62, 50)
}