/
rapi.go
40 lines (31 loc) · 858 Bytes
/
rapi.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
package crypto
import (
"fmt"
"io"
"io/ioutil"
)
func (k *Key) Decrypt(reader io.Reader) ([]byte, error) {
buf, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
// decrypt
nonce, ciphertext := buf[:k.NonceSize()], buf[k.NonceSize():]
plaintext, err := k.Open(ciphertext[:0], nonce, ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("decryption failed: %v", err)
}
return plaintext, nil
}
// Encrypt encrypts plaintext data and returns the ciphertext as a byte array
func (k *Key) Encrypt(data []byte) []byte {
nonce := NewRandomNonce()
ciphertext := make([]byte, 0, ciphertextLength(len(data)))
ciphertext = append(ciphertext, nonce...)
// encrypt blob
ciphertext = k.Seal(ciphertext, nonce, data, nil)
return ciphertext
}
func ciphertextLength(plaintextSize int) int {
return plaintextSize + Extension
}