/
crypto.go
80 lines (68 loc) · 2.09 KB
/
crypto.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 crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"github.com/xmlking/micro-starter-kit/shared/util"
)
// AesEncrypt takes in a string, a key and returns encrypted string
func AesEncrypt(orig string, key string) (string, error) {
// generate a new aes cipher using our 32 byte long key
c, err := aes.NewCipher([]byte(key))
// if there are any errors, handle them
if err != nil {
return "", err
}
// gcm or Galois/Counter Mode, is a mode of operation
// for symmetric key cryptographic block ciphers
// - https://en.wikipedia.org/wiki/Galois/Counter_Mode
gcm, err := cipher.NewGCM(c)
// if any error generating new GCM
// handle them
if err != nil {
return "", err
}
// creates a new byte array the size of the nonce
// which must be passed to Seal
nonce := make([]byte, gcm.NonceSize())
// populates our nonce with a cryptographically secure
// random sequence
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
// here we encrypt our text using the Seal function
// Seal encrypts and authenticates plaintext, authenticates the
// additional data and appends the result to dst, returning the updated
// slice. The nonce must be NonceSize() bytes long and unique for all
// time, for a given key.
// signatured := commonutils.HexEncodeToString()
return util.Base64Encode(gcm.Seal(nonce, nonce, []byte(orig), nil)), nil
}
// AesDecrypt takes in a encripted string, a key and returns decrypted string
func AesDecrypt(cryted string, key string) (string, error) {
ciphertext, err := util.Base64Decode(cryted)
// if our program was unable to read the file
// print out the reason why it can't
if err != nil {
return "", err
}
c, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return "", err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return "", err
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}
return string(plaintext), nil
}