-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.go
74 lines (68 loc) · 2.37 KB
/
util.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
package crypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"io"
)
// GenerateRandomBytes returns securely generated random bytes.
// It will return an error if the system's secure random
// number generator fails to function correctly.
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
b = nil
}
return b, err
}
// GenerateRandomBase64String returns a base64 encoded
// securely generated random string. The given value is the length of the initial random bytes represented by
// the base64 string. The actual length of the string will be bigger.
func GenerateRandomBase64String(s int) (string, error) {
b, err := GenerateRandomBytes(s)
return base64.StdEncoding.EncodeToString(b), err
}
// Encrypt will encrypt the data using AES-GCM and return the encrypted data.
// Use Decrypt to decrypt the data.
// key is recommended to be 16 bytes long, and will produce an AES-128 encryption using GCM
// which is currently considered secure. Note that this is due to our use of GCM. When using CBC,
// AES-256 is considered secure, but GCM is better and faster.
// Errors will panic, since they are caused by some kind of system-wide failure or a bad key size.
func Encrypt(data []byte, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}
ciphertext := gcm.Seal(nonce, nonce, data, nil) // Here we append the encrypted text to the nonce.
return ciphertext
}
// Decrypt will decrypt something created by Encrypt. If the data is not decryptable, either because it is corrupt,
// or the key changed, an error is returned. Otherwise, if there is a system failure, it will panic.
func Decrypt(data []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonceSize := gcm.NonceSize()
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
ret,err := gcm.Open(nil, nonce, ciphertext, nil)
if ret == nil && err == nil {
return []byte{}, nil // encrypted an empty array to begin with
}
return ret, err
}