-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptoservice.go
92 lines (75 loc) · 2.32 KB
/
cryptoservice.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
package vbcore
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
)
// CryptoService provides a clean interface to encrypt data
type CryptoService struct {
gcm cipher.AEAD
}
// NewCryptoService generates a new `CryptoService` instance used to perform
// cryptographically secure encryption and decryption processes using `AES256`
// in `GCM` (Galois-Counter-Mode).
func NewCryptoService(key []byte) (cs *CryptoService, err error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return &CryptoService{
gcm: gcm,
}, nil
}
// Encrypt takes the plain content, encrypts it and returns the cipher already
// including the used iv
func (cs CryptoService) Encrypt(plain []byte) (cipher []byte, err error) {
if len(plain) == 0 {
return nil, errors.New("vbcore/CryptoService: no content to encrypt")
}
nonce, err := CryptoGenBytes(cs.gcm.NonceSize())
if err != nil {
return nil, err
}
cipher = cs.gcm.Seal(nil, nonce, plain, nil)
cipher = append(nonce, cipher...)
return cipher, nil
}
// EncryptBase64 works like `Encrypt`, but encodes the cipher to it's `base64`
// equivalent before returning.
func (cs CryptoService) EncryptBase64(plain []byte) (base64Cipher []byte, err error) {
cipher, err := cs.Encrypt(plain)
if err != nil {
return nil, err
}
base64Cipher = make([]byte, base64.RawStdEncoding.EncodedLen(len(cipher)))
base64.RawStdEncoding.Encode(base64Cipher, cipher)
return
}
// Decrypt takes the cipher content, decrypts it and returns the plain text
func (cs CryptoService) Decrypt(cipher []byte) (plain []byte, err error) {
if len(cipher) == 0 {
return nil, errors.New("vbcore/CryptoService: no content to decrypt")
}
nonce := cipher[0:cs.gcm.NonceSize()]
ciphertext := cipher[cs.gcm.NonceSize():]
plain, err = cs.gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return
}
// DecryptBase64 works like `Decrypt`, but decodes the cipher to it's non
// `base64` equivalent before decrypting.
func (cs CryptoService) DecryptBase64(base64Cipher []byte) (plain []byte, err error) {
cipher := make([]byte, base64.RawStdEncoding.DecodedLen(len(base64Cipher)))
_, err = base64.RawStdEncoding.Decode(cipher, base64Cipher)
if err != nil {
return nil, err
}
return cs.Decrypt(cipher)
}