/
encrypt.go
46 lines (37 loc) · 1.06 KB
/
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
43
44
45
46
package main
import (
"crypto/rand"
"crypto/sha256"
"errors"
"golang.org/x/crypto/chacha20poly1305"
)
func encrypt(plaintext []byte, pass string) ([]byte, error) {
key := sha256.Sum256([]byte(pass))
aead, err := chacha20poly1305.NewX(key[:])
if err != nil {
return nil, err
}
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(plaintext)+aead.Overhead())
if _, err := rand.Read(nonce); err != nil {
return nil, err
}
return aead.Seal(nonce, nonce, plaintext, nil), nil
}
func decrypt(encryptedMsg []byte, pass string) ([]byte, error) {
key := sha256.Sum256([]byte(pass))
aead, err := chacha20poly1305.NewX(key[:])
if err != nil {
return nil, err
}
if len(encryptedMsg) < aead.NonceSize() {
return nil, errors.New("ciphertext too short")
}
// Split nonce and ciphertext.
nonce, ciphertext := encryptedMsg[:aead.NonceSize()], encryptedMsg[aead.NonceSize():]
// Decrypt the message and check it wasn't tampered with.
plaintext, err := aead.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}