This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
forked from wnfs-wg/wnfs-go
/
key.go
147 lines (120 loc) · 2.79 KB
/
key.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package private
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sync"
cid "github.com/ipfs/go-cid"
"github.com/functionland/wnfs-go/base"
ratchet "github.com/functionland/wnfs-go/private/ratchet"
)
var EmptyKey = Key([32]byte{})
type Key [32]byte
func NewKey() Key {
return ratchet.NewSpiral().Key()
}
func (k Key) Encode() string { return base64.URLEncoding.EncodeToString(k[:]) }
func (k *Key) Decode(s string) error {
data, err := base64.URLEncoding.DecodeString(s)
if err != nil {
return err
}
for i, d := range data {
k[i] = d
}
return nil
}
func (k Key) IsEmpty() bool { return k == EmptyKey }
func (k Key) MarshalJSON() ([]byte, error) {
return []byte(`"` + k.Encode() + `"`), nil
}
func (k *Key) UnmarshalJSON(d []byte) error {
var s string
if err := json.Unmarshal(d, &s); err != nil {
return err
}
return k.Decode(s)
}
type DecryptionStore interface {
DecryptionFields(id cid.Cid) (sname Name, key Key, err error)
}
type WritableDecryptionStore interface {
DecryptionStore
PutDecryptionFields(id cid.Cid, sname Name, key Key) error
}
func NewDecryptionStore(filepath string) (WritableDecryptionStore, error) {
s := &decryptionStore{
path: filepath,
}
return s, s.load()
}
type decryptionStore struct {
path string
sync.Mutex
cache map[cid.Cid]decryption
}
type decryption struct {
Name
Key
}
var _ WritableDecryptionStore = (*decryptionStore)(nil)
func (s *decryptionStore) PutDecryptionFields(id cid.Cid, name Name, key Key) error {
s.Lock()
defer s.Unlock()
s.cache[id] = decryption{name, key}
return s.write()
}
func (s *decryptionStore) DecryptionFields(id cid.Cid) (name Name, key Key, err error) {
s.Lock()
defer s.Unlock()
if dec, ok := s.cache[id]; ok {
return dec.Name, dec.Key, nil
}
return name, key, base.ErrNotFound
}
func (s *decryptionStore) load() error {
s.cache = map[cid.Cid]decryption{}
data, err := ioutil.ReadFile(s.path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
strs := map[string]map[string]string{}
if err := json.Unmarshal(data, &strs); err != nil {
return err
}
for idstr, dec := range strs {
id, err := cid.Parse(idstr)
if err != nil {
return fmt.Errorf("parsing cid %s: %w", idstr, err)
}
keystr := dec["key"]
k := &Key{}
if err = k.Decode(keystr); err != nil {
return fmt.Errorf("parsing key %s: %w", keystr, err)
}
s.cache[id] = decryption{
Name: Name(dec["name"]),
Key: *k,
}
}
return nil
}
func (s *decryptionStore) write() error {
asStrings := map[string]interface{}{}
for id, dec := range s.cache {
asStrings[id.String()] = map[string]interface{}{
"name": string(dec.Name),
"key": dec.Key.Encode(),
}
}
data, err := json.Marshal(asStrings)
if err != nil {
return err
}
return ioutil.WriteFile(s.path, data, 0644)
}