-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaesgcm.go
109 lines (92 loc) · 1.99 KB
/
aesgcm.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
/*!
* go-rs/aesgcm
* Copyright(c) 2019 Roshan Gade
* MIT Licensed
*/
package crypto
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
)
// Reference: https://golang.org/pkg/crypto/cipher
// Reference: https://software.intel.com/en-us/articles/aes-gcm-encryption-performance-on-intel-xeon-e5-v3-processors
type AESGCM struct {
aesgcm cipher.AEAD
nonce []byte // common salt/nonce
}
// The key argument should be the AES key, either 16 or 32 bytes
// to select AES-128 or AES-256.
// salt/nonce should be optional
func (c *AESGCM) Config(cipherKey string, salt string) (err error) {
var block cipher.Block
block, err = aes.NewCipher([]byte(cipherKey))
if err != nil {
return
}
// should use NewGCM, which is more resistant to misuse
c.aesgcm, err = cipher.NewGCM(block)
if err != nil {
return
}
if salt != "" {
c.nonce, err = hex.DecodeString(salt)
if err != nil {
return
}
}
return
}
/**
* Encrypt text
*/
func (c *AESGCM) Encrypt(text string) (val string, err error) {
val = hex.EncodeToString(c.aesgcm.Seal(nil, c.nonce, []byte(text), nil))
return
}
/**
* Encrypt text with nonce
*/
func (c *AESGCM) EncryptWithNonce(text string, nonce string) (val string, err error) {
data := []byte(text)
_nonce, err := hex.DecodeString(nonce)
if err != nil {
return
}
val = hex.EncodeToString(c.aesgcm.Seal(nil, _nonce, data, nil))
return
}
/**
* Decrypt string
*/
func (c *AESGCM) Decrypt(text string) (val string, err error) {
data, err := hex.DecodeString(text)
if err != nil {
return "", err
}
data, err = c.aesgcm.Open(nil, c.nonce, data, nil)
if err != nil {
return
}
val = string(data)
return
}
/**
* Decrypt string with nonce
*/
func (c *AESGCM) DecryptWithNonce(text string, nonce string) (val string, err error) {
data, err := hex.DecodeString(text)
if err != nil {
return
}
_nonce, err := hex.DecodeString(nonce)
if err != nil {
return
}
data, err = c.aesgcm.Open(nil, _nonce, data, nil)
if err != nil {
return
}
val = string(data)
return
}