-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
84 lines (70 loc) · 1.97 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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"io"
"github.com/hammertrack/tracker/errors"
"github.com/hammertrack/tracker/utils"
)
var ErrInvalidLength = errors.New("aead: invalid length")
func encrypt(plaintext, secret []byte) ([]byte, error) {
block, err := aes.NewCipher(secret)
if err != nil {
return nil, errors.Wrap(err)
}
aead, err := cipher.NewGCM(block)
if err != nil {
return nil, errors.Wrap(err)
}
nonce := make([]byte, aead.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, errors.Wrap(err)
}
b := aead.Seal(nonce, nonce, plaintext, nil)
return b64enc(b), nil
}
func decrypt(enc string, secret []byte) ([]byte, error) {
data, err := b64dec(enc)
if err != nil {
return nil, errors.Wrap(err)
}
block, err := aes.NewCipher(secret)
if err != nil {
return nil, errors.Wrap(err)
}
aead, err := cipher.NewGCM(block)
if err != nil {
return nil, errors.Wrap(err)
}
l := aead.NonceSize()
if len(data) < l {
return nil, errors.Wrap(ErrInvalidLength)
}
nonce, plaintext := data[:l], data[l:]
b, err := aead.Open(nil, nonce, plaintext, nil)
if err != nil {
return nil, errors.Wrap(err)
}
return b, nil
}
func b64enc(src []byte) []byte {
// TODO - maybe we could minimize memory allocations reusing buffers from
// other cursors by using a pool of buffers for b64 encoding/decoding, but
// this buffer pool should be used at a higher scope where we control the
// lifespan of dst so we can put it back to the pool. Which requires
// restructuring this, encrypt/decrypt and cursor
dst := make([]byte, base64.URLEncoding.EncodedLen(len(src)))
base64.URLEncoding.Encode(dst, src)
return dst
}
func b64dec(data string) ([]byte, error) {
dst := make([]byte, len(data))
// Saves an extra allocation in this hotpath by no using DecodeString
n, err := base64.URLEncoding.Decode(dst, utils.StrToByte(data))
if err != nil {
return nil, errors.Wrap(err)
}
return dst[:n], nil
}