Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/utils/random/random.go
1562 views
1
package random
2
3
import (
4
"crypto/rand"
5
"math/big"
6
mathRand "math/rand"
7
"time"
8
9
"github.com/google/uuid"
10
)
11
12
var Rand *mathRand.Rand
13
14
const letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
15
16
func String(n int) string {
17
b := make([]byte, n)
18
letterLen := big.NewInt(int64(len(letterBytes)))
19
for i := range b {
20
idx, err := rand.Int(rand.Reader, letterLen)
21
if err != nil {
22
panic(err)
23
}
24
b[i] = letterBytes[idx.Int64()]
25
}
26
return string(b)
27
}
28
29
func Token() string {
30
return "alist-" + uuid.NewString() + String(64)
31
}
32
33
func RangeInt64(left, right int64) int64 {
34
return mathRand.Int63n(left+right) - left
35
}
36
37
func init() {
38
s := mathRand.NewSource(time.Now().UnixNano())
39
Rand = mathRand.New(s)
40
}
41
42