forked from cloudfoundry-community/rds-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
63 lines (47 loc) · 1.24 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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
)
func randStr(strSize int) string {
var dictionary string
dictionary = "0123456789abcdefghijklmnopqrstuvwxyz"
bytes := GenerateIv(strSize)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}
func Encrypt(msg, key string, iv []byte) (string, error) {
src := []byte(msg)
dst := make([]byte, len(src))
aesBlockEncrypter, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv)
aesEncrypter.XORKeyStream(dst, src)
return base64.StdEncoding.EncodeToString(dst), nil
}
func Decrypt(msg, key string, iv []byte) (string, error) {
src, _ := base64.StdEncoding.DecodeString(msg)
dst := make([]byte, len(src))
aesBlockDecrypter, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
aesDecrypter := cipher.NewCFBDecrypter(aesBlockDecrypter, iv)
aesDecrypter.XORKeyStream(dst, src)
return string(dst), nil
}
func GenerateIv(size int) []byte {
var bytes = make([]byte, size)
rand.Read(bytes)
return bytes
}
func GenerateSalt(size int) string {
iv := GenerateIv(size)
return base64.StdEncoding.EncodeToString(iv)
}