1package export
2
3import (
4 "crypto/rand"
5 "io"
6 "os"
7)
8
9const (
10 KeySize = 32
11)
12
13func CreateKey(path string) error {
14 var key [KeySize]byte
15 _, err := rand.Read(key[:])
16 if err != nil {
17 return err
18 }
19
20 file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
21 if err != nil {
22 return err
23 }
24 defer file.Close()
25
26 // Write and reset offset for read in ReadKey() function.
27 _, err = file.Write(key[:])
28 if err != nil {
29 return err
30 }
31
32 return nil
33}
34
35func ReadKey(path string) (*[KeySize]byte, error) {
36 file, err := os.Open(path)
37 if err != nil {
38 return nil, err
39 }
40 defer file.Close()
41
42 var key [KeySize]byte
43 _, err = io.ReadFull(file, key[:])
44 if err != nil {
45 return nil, err
46 }
47
48 return &key, nil
49}