-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret.go
52 lines (43 loc) · 997 Bytes
/
secret.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
package main
import (
"encoding/hex"
"io"
"crypto/rand"
"crypto/aes"
)
type Secret struct {
key []byte
nonce []byte
aesecb *ecb
}
func NewSecret(key string) (*Secret, error) {
secret := new(Secret)
k, err := hex.DecodeString(key)
if err != nil {
return nil, err
}
secret.key = k
// Generate nonce.
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
secret.nonce = nonce
// Set block and cipher
block, err := aes.NewCipher(secret.key)
if err != nil {
panic(err.Error())
}
secret.aesecb = newECB(block)
return secret, nil
}
func (secret *Secret) Encrypt(value []byte) {
//return secret.aesecb.Seal(nil, secret.nonce, []byte(value), nil)
encrypter := ecbEncrypter(*secret.aesecb)
encrypter.CryptBlocks(value, value)
}
func (secret *Secret) Decrypt(value []byte) {
//return secret.aesgcm.Open(nil, secret.nonce, value, nil)
decrypter := ecbDecrypter(*secret.aesecb)
decrypter.CryptBlocks(value, value)
}