-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaes.go
83 lines (68 loc) · 2.25 KB
/
aes.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
package gocrypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"github.com/pkg/errors"
)
// AESOpt contains all aes session option
type AESOpt struct {
aesGCM cipher.AEAD
}
// NewAESOpt is function to create new configuration of aes algorithm option
// the secret must be hexa a-f & 0-9
func NewAESOpt(secret string) (*AESOpt, error) {
if len(secret) != 64 {
return nil, errors.New("Secret must be 64 character")
}
key, err := hex.DecodeString(secret)
if err != nil {
return nil, errors.Wrap(err, "NewAESOpt.hex.DecodeString")
}
//Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
return nil, errors.Wrap(err, "NewAESOpt.aes.NewCipher")
}
//Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode
//https://golang.org/pkg/crypto/cipher/#NewGCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return nil, errors.Wrap(err, "NewAESOpt.cipher.NewGCM")
}
return &AESOpt{
aesGCM: aesGCM,
}, nil
}
// Encrypt is function to encrypt data using AES algorithm
func (aesOpt *AESOpt) Encrypt(plainText []byte) (string, error) {
//Create a nonce. Nonce should be from GCM
nonce := make([]byte, aesOpt.aesGCM.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", errors.Wrap(err, "encryptAES.io.ReadFull")
}
//Encrypt the data using aesGCM.Seal
//Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix.
ciphertext := aesOpt.aesGCM.Seal(nonce, nonce, plainText, nil)
return fmt.Sprintf("%x", ciphertext), nil
}
// Decrypt is function to decypt data using AES algorithm
func (aesOpt *AESOpt) Decrypt(chiperText []byte) (string, error) {
enc, _ := hex.DecodeString(string(chiperText))
//Get the nonce size
nonceSize := aesOpt.aesGCM.NonceSize()
if len(enc) < nonceSize {
return "", errors.New("The data can't be decrypted")
}
//Extract the nonce from the encrypted data
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]
//Decrypt the data
plainText, err := aesOpt.aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", errors.Wrap(err, "decryptAES.aesGCM.Open")
}
return fmt.Sprintf("%s", plainText), nil
}