-
Notifications
You must be signed in to change notification settings - Fork 0
/
seal.go
52 lines (44 loc) · 941 Bytes
/
seal.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
package main
import (
"errors"
"io/ioutil"
"math/rand"
"os"
"golang.org/x/crypto/nacl/secretbox"
)
func init() {
registerHelp("seal",
`usage: box seal plain.txt > cipher.txt
Seal reads a plaintext file, encrypts it, and writes the encrypted bytes to stdout.
The environment variable BOX_KEY must be set to a valid secretbox key.
`)
}
func cmdSeal(key *[32]byte, inFile string) error {
if inFile == "" {
return errors.New("usage: box seal plain.txt > cipher.txt")
}
f, err := os.Open(inFile)
if err != nil {
return err
}
defer f.Close()
pb, err := ioutil.ReadAll(f)
if err != nil {
return err
}
cb, err := encryptBytes(key, pb)
if err != nil {
return err
}
os.Stdout.Write(cb)
return nil
}
func encryptBytes(k *[32]byte, b []byte) ([]byte, error) {
var nonce [24]byte
_, err := rand.Read(nonce[:])
if err != nil {
return nil, err
}
out := secretbox.Seal(nonce[:], b, &nonce, k)
return out, nil
}