-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
113 lines (88 loc) · 2.64 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
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
package goravel
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"io"
"os"
)
// CreateDirIfNotExists creates a new directory if it does not exist
func (g *Goravel) CreateDirIfNotExists(path string) error {
_, err := os.Stat(path)
if os.IsNotExist(err) {
err = os.Mkdir(path, 0755) // default permissions
if err != nil {
return err
}
}
return nil
}
// CreateFileIfNotExists creates a new file at path if it does not exist
func (g *Goravel) CreateFileIfNotExists(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
var file, err = os.Create(path)
if err != nil {
return err
}
defer func(file *os.File) {
_ = file.Close()
}(file)
}
return nil
}
const (
randomString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321_+"
)
// RandomString generates a random string of length n from values in the const randomString
func (g *Goravel) RandomString(n int) string {
// Create a slice s of runes with length n
s, r := make([]rune, n), []rune(randomString)
// Loop through each index of slice s
for i := range s {
// Generate a random prime number p with bit length equal to the length of r
p, _ := rand.Prime(rand.Reader, len(r))
// Get the uint64 representation of the prime number
x, y := p.Uint64(), uint64(len(r))
// Use the modulo operation to ensure the index is within bounds of r
s[i] = r[x%y]
}
// Convert the slice of runes s to a string and return it
return string(s)
}
type Encryption struct {
Key []byte
}
// Encrypt encrypts a string using the AES encryption algorithm
func (e *Encryption) Encrypt(text string) (string, error) {
plaintext := []byte(text)
block, err := aes.NewCipher(e.Key)
if err != nil {
return "", err
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize] // Initialization vector
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
// Encode the encrypted text to base64
return base64.URLEncoding.EncodeToString(ciphertext), nil
}
// Decrypt decrypts a string using the AES encryption algorithm
func (e *Encryption) Decrypt(cryptoText string) (string, error) {
encetptedText, _ := base64.URLEncoding.DecodeString(cryptoText)
block, err := aes.NewCipher(e.Key)
if err != nil {
return "", err
}
if len(encetptedText) < aes.BlockSize {
return "", err
}
iv := encetptedText[:aes.BlockSize]
ciphertext := encetptedText[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)
return string(ciphertext), nil
}