-
Notifications
You must be signed in to change notification settings - Fork 13
/
simple.go
79 lines (64 loc) · 1.72 KB
/
simple.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
package generator
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"math/big"
"strings"
"github.com/AmirSoleimani/VoucherCodeGenerator/vcgen"
)
type SimpleGenerator struct {
}
func (g *SimpleGenerator) GenerateRandomRangeNumber(min, max int64) int64 {
bg := big.NewInt(max - min)
n, err := rand.Int(rand.Reader, bg)
if err != nil {
panic(err)
}
return n.Int64() + min
}
func (g *SimpleGenerator) GenerateSha256Hash(input string) string {
sha256Hash := sha256.New()
sha256Hash.Write([]byte(input))
return base64.StdEncoding.EncodeToString(sha256Hash.Sum(nil))
}
func (g *SimpleGenerator) GenerateRandomCode(generator *vcgen.Generator) string {
vc := vcgen.New(generator)
result, err := vc.Run()
if err != nil {
panic(err)
}
return strings.Join(*result, "")
}
const (
lowerCharSet = "abcdefghijklmnopqrstuvwxyz"
upperCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
specialCharSet = "!@#$%*-+.?"
numberSet = "0123456789"
allCharSet = lowerCharSet + upperCharSet + specialCharSet + numberSet
)
func (g *SimpleGenerator) RandomPasswordGenerator(passwordLength int) string {
var password strings.Builder
max := big.NewInt(int64(len(allCharSet)))
for i := 0; i < passwordLength; i++ {
num, err := rand.Int(rand.Reader, max)
if err != nil {
panic(err)
}
password.WriteRune(rune(allCharSet[num.Int64()]))
}
return password.String()
}
func (g *SimpleGenerator) RandomPINCodeGenerator(codeLength int) string {
charSet := lowerCharSet + numberSet
var code strings.Builder
max := big.NewInt(int64(len(charSet)))
for i := 0; i < codeLength; i++ {
num, err := rand.Int(rand.Reader, max)
if err != nil {
panic(err)
}
code.WriteRune(rune(charSet[num.Int64()]))
}
return code.String()
}