/
util.go
44 lines (36 loc) · 1.02 KB
/
util.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
package oracle
import (
"errors"
"io"
"os"
"golang.org/x/crypto/chacha20poly1305"
)
var ErrNoEphemeralKey = errors.New("no ephemeral key")
var UniversalNonce []byte = make([]byte, chacha20poly1305.NonceSize)
const GLOBAL_SALT = "oracle/v1"
// key is a one-time ephemeral key
// therefore, nonce can (and should) be just a bunch of zeros (UniversalNonce)
func aeadEncrypt(key, plaintext []byte) ([]byte, error) {
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
return aead.Seal(nil, UniversalNonce, plaintext, nil), nil
}
func aeadDecrypt(key []byte, ciphertext []byte) ([]byte, error) {
aead, err := chacha20poly1305.New(key)
if err != nil {
return nil, err
}
return aead.Open(nil, UniversalNonce, ciphertext, nil)
}
// Flarg represents all the args and flags after normalization and validation
type Flarg struct {
Subcommand string
ConfigFile *os.File
Format string
InputStream io.Reader
OutputStream io.Writer
}
// The NoFlarg Flarg is used in error conditions
var NoFlarg Flarg