-
Notifications
You must be signed in to change notification settings - Fork 115
/
crypto.go
72 lines (57 loc) · 1.9 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
package authn
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"fmt"
)
// AES-256 encryption.
type cryptographer struct {
gcm cipher.AEAD
}
func newCryptographer(passphrase string) (*cryptographer, error) {
if len(passphrase) == 0 {
return nil, fmt.Errorf("passphrase was empty")
}
// Hash the passphrase with SHA-256. Alternatively we could have required a 64-byte hex string.
// The other option would be a 32-byte key, but entropy with a 32-byte user-provided key is likely to be lower since
// use of non-printable characters is unlikely.
key := sha256.Sum256([]byte(passphrase))
cb, err := aes.NewCipher(key[:])
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(cb)
if err != nil {
return nil, err
}
return &cryptographer{gcm: gcm}, nil
}
// Return decrypted bytes using input with nonce as the prefix and ciphertext as remaining bytes.
func (c *cryptographer) Decrypt(b []byte) ([]byte, error) {
// Ensure we don't panic when splitting the nonce and ciphertext.
if len(b) < c.gcm.NonceSize() {
return nil, fmt.Errorf("invalid nonce+cipher, bytes for decryption are smaller than algorithm's nonce size")
}
nonce, ciphertext := b[:c.gcm.NonceSize()], b[c.gcm.NonceSize():]
out, err := c.gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return out, nil
}
// Return an encrypted input bytes with nonce as the prefix.
func (c *cryptographer) Encrypt(b []byte) ([]byte, error) {
if len(b) == 0 {
return nil, fmt.Errorf("input bytes were empty, could not encrypt")
}
nonce := make([]byte, c.gcm.NonceSize())
if _, err := rand.Read(nonce); err != nil {
return nil, fmt.Errorf("error generating nonce: %w", err)
}
// Prefix output with nonce, allocate capacity using provided overhead.
out := make([]byte, c.gcm.NonceSize(), c.gcm.NonceSize()+c.gcm.Overhead()+len(b))
copy(out, nonce)
return c.gcm.Seal(out, nonce, b, nil), nil
}