-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
42 lines (34 loc) · 932 Bytes
/
encrypt.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"golang.org/x/crypto/scrypt"
)
// Encrypt encrypts plaintext using the provided password and returns the
// encrypted text, nonce, salt, and an error if any.
func Encrypt(plaintext, password string) ([]byte, []byte, []byte, error) {
salt := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
return nil, nil, nil, err
}
key, err := scrypt.Key([]byte(password), salt, 32768, 8, 1, 32)
if err != nil {
return nil, nil, nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, nil, nil, err
}
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, nil, nil, err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, nil, nil, err
}
ciphertext := aesgcm.Seal(nil, nonce, []byte(plaintext), nil)
return ciphertext, nonce, salt, nil
}