/
file.go
46 lines (38 loc) · 967 Bytes
/
file.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 encrypteddb
import (
"os"
"github.com/adamwalz/keybase-client/go/libkb"
"golang.org/x/net/context"
)
type EncryptedFile struct {
libkb.Contextified
getSecretBoxKey KeyFn
path string
}
func NewFile(g *libkb.GlobalContext, path string, getSecretBoxKey KeyFn) *EncryptedFile {
return &EncryptedFile{
Contextified: libkb.NewContextified(g),
path: path,
getSecretBoxKey: getSecretBoxKey,
}
}
func (f *EncryptedFile) Get(ctx context.Context, res interface{}) error {
enc, err := os.ReadFile(f.path)
if err != nil {
return err
}
if err = DecodeBox(ctx, enc, f.getSecretBoxKey, res); err != nil {
return err
}
return nil
}
func (f *EncryptedFile) Put(ctx context.Context, data interface{}) error {
b, err := EncodeBox(ctx, data, f.getSecretBoxKey)
if err != nil {
return err
}
return os.WriteFile(f.path, b, 0644)
}
func (f *EncryptedFile) Remove(ctx context.Context) error {
return os.Remove(f.path)
}