forked from lonng/nano
/
crypto.go
117 lines (93 loc) · 2.13 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
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
114
115
116
117
package cryptocodec
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
)
type Method int
const (
AES128GCM Method = iota
AES256GCM
)
var methodNameMap = map[Method]string{
AES128GCM: "aes-128-gcm",
AES256GCM: "aes-256-gcm",
}
func (m Method) String() string {
if name, ok := methodNameMap[m]; ok {
return name
}
return fmt.Sprintf("unknown method: %d", m)
}
type Crypto struct {
Method Method
Key []byte
}
func NewCrypto(method Method, key []byte) (*Crypto, error) {
c := &Crypto{
Method: method,
Key: key,
}
return c, nil
}
func (c *Crypto) Encode(data []byte) ([]byte, error) {
switch c.Method {
case AES128GCM, AES256GCM:
return aesgcm.Encrypt(data, c.Key)
default:
return nil, fmt.Errorf("unknown method: %s", c.Method)
}
}
func (c *Crypto) Decode(data []byte) ([]byte, error) {
switch c.Method {
case AES128GCM, AES256GCM:
return aesgcm.Decrypt(data, c.Key)
default:
return nil, fmt.Errorf("unknown method: %s", c.Method)
}
}
type AESGCM struct{}
var aesgcm = &AESGCM{}
func (*AESGCM) Encrypt(plaintext, key []byte) ([]byte, error) {
nonce := make([]byte, 12)
if _, err := rand.Read(nonce); err != nil {
return nil, err
}
// test code:
// nonce := []byte("123456789012")
return aesgcm.EncryptWithNonce(plaintext, key, nonce)
}
func (*AESGCM) EncryptWithNonce(plaintext, key []byte, nonce []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
ad := aesgcm.Seal(nil, nonce, nonce, nonce)
ciphertext := aesgcm.Seal(nil, nonce, plaintext, ad)
return append(nonce, ciphertext...), nil
}
func (*AESGCM) Decrypt(ciphertext, key []byte) ([]byte, error) {
if len(ciphertext) < 12 {
return nil, fmt.Errorf("invalid ciphertext")
}
nonce := ciphertext[:12]
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
ad := aesgcm.Seal(nil, nonce, nonce, nonce)
plaintext, err := aesgcm.Open(nil, nonce, ciphertext[12:], ad)
if err != nil {
return nil, err
}
return plaintext, nil
}