-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
80 lines (66 loc) · 1.45 KB
/
helpers.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
package oxicrypt
import (
"errors"
"log"
"math/rand"
"strings"
)
func formError(errorID string, errorText ...string) error {
var finalText string
for i, errorStr := range errorText {
if i == 0 {
finalText = errorStr
} else {
finalText = finalText + ", " + errorStr
}
}
return errors.New(errorID + ": " + finalText)
}
const cAlphaNum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+=-[]{};:|,./<>?~"
const cMaxLen = len(cAlphaNum)
func generateRandomString(lenFrom, lenTo int) string {
lenMax := lenTo - lenFrom
if lenMax <= 0 {
return ""
}
lenMax = rand.Intn(lenMax) + lenFrom
log.Printf("LenMax: %d", lenMax)
i := 0
var builder strings.Builder
for i < lenMax {
randI := rand.Intn(cMaxLen)
symbol := cAlphaNum[randI]
_, err := builder.WriteString(string(symbol))
if err != nil {
log.Println(err)
return builder.String()
}
i++
}
return builder.String()
}
func generateSalt() string {
var builder strings.Builder
i := 0
for i < cSaltLength {
randI := rand.Intn(cMaxLen)
symbol := cAlphaNum[randI]
_, err := builder.WriteString(string(symbol))
if err != nil {
log.Println(err)
return builder.String()
}
i++
}
return builder.String()
}
func generateRandomBytesWithRandomLen(lenFrom, lenTo int) []byte {
lenMax := lenTo - lenFrom
if lenMax <= 0 {
return nil
}
lenMax = rand.Intn(lenMax) + lenFrom
var bytes = make([]byte, lenMax)
rand.Read(bytes)
return bytes
}