-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
221 lines (178 loc) · 4.5 KB
/
client.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package sse
import (
"encoding/json"
"math"
"strconv"
)
type Client struct {
DB DBConn
key []byte
}
const (
BlobSize = 10 // Size of array holding doc IDs
)
var One = []byte{0x01} // Needed for different keys.
var Two = []byte{0x02}
func NewClient(db DBConn) (*Client, error) {
err := db.Init()
return &Client{DB: db}, err
}
// Get a document back from the store with the given ID.
func (c *Client) Get(id string) ([]byte, error) {
// Get the encrypted doc from the database.
edoc, err := c.DB.Get(DOCUMENTS, []byte(id))
// Decrypt the doc with the client key.
doc, err := Decrypt(edoc, c.key)
return doc, err
}
// Put a document into the store with the given ID.
func (c *Client) Put(id string, doc []byte) error {
// Let's encrypt the document with the client key.
edoc, err := Encrypt(doc, c.key)
if err != nil {
return err
}
// Now that it's encrypted, store it in the DB.
err = c.DB.Put(DOCUMENTS, []byte(id), edoc)
return err
}
// Get a document back from the store with the given ID.
func (c *Client) Delete(id string) error {
// Get the encrypted doc from the database.
return c.DB.Delete(DOCUMENTS, []byte(id))
}
// Get the current count value for a given keyword.
func (c *Client) Count(keyword string) (int, error) {
// HMAC the keyword.
ekey := HMAC([]byte(keyword), c.key)
count, err := c.DB.Get(COUNTS, ekey)
if err != nil {
return 0, err
}
var i int
if len(count) == 0 {
i = 0
err := c.SetCount(keyword, i)
if err != nil {
return 0, err
}
} else {
i, err = strconv.Atoi(string(count))
if err != nil {
return 0, err
}
}
return i, nil
}
func (c *Client) SetCount(keyword string, count int) error {
// HMAC the keyword.
ekey := HMAC([]byte(keyword), c.key)
i := strconv.Itoa(count)
err := c.DB.Put(COUNTS, ekey, []byte(i))
return err
}
// Find all document IDs associated with the given keyword.
func (c *Client) Search(keyword string) (ids []string, err error) {
kw1 := append([]byte(keyword), One[:]...) // We append a constant to each keyword before MAC
kw2 := append([]byte(keyword), Two[:]...)
k1 := HMAC(kw1, c.key) // Generate two separate keys.
k2 := HMAC(kw2, c.key)
// Get count
count, err := c.Count(keyword)
if err != nil {
return
}
max := int(math.Floor(float64(count) / float64(BlobSize)))
if count%BlobSize == 0 {
max = max - 1
}
for i := 0; i <= max; i++ {
// Generate the id of this block using k1.
h := HMAC(append([]byte("COUNT"), byte(i)), k1)
// Get the encrypted blob.
ejson, err2 := c.DB.Get(INDEX, h)
if err2 != nil {
err = err2
return
}
// Decrypt the blob with k2.
pjson, err2 := Decrypt(ejson, k2)
if err2 != nil {
err = err2
return
}
var block []string
err = json.Unmarshal(pjson, &block)
if err != nil {
return
}
ids = append(ids, block...)
}
return
}
/*
// TODO: Use an argument list here or not? (ie. keywords ...string)
func (c *Client) AddDocsToKeyword(keyword string, docs []string) error {
}
// TODO: Use an argument list here or not? (ie. keywords ...string)
func (c *Client) AddKeywordsToDoc(doc string, keywords []string) error {
}
*/
func (c *Client) AddDocToKeyword(keyword, doc string) error {
kw1 := append([]byte(keyword), One[:]...) // We append a constant to each keyword before MAC
kw2 := append([]byte(keyword), Two[:]...)
k1 := HMAC(kw1, c.key) // Generate two separate keys.
k2 := HMAC(kw2, c.key)
// Get count
count, err := c.Count(keyword)
if err != nil {
return err
}
max := int(math.Floor(float64(count) / float64(BlobSize)))
// Generate the id of this block using k1.
h := HMAC(append([]byte("COUNT"), byte(max)), k1)
// Get the encrypted blob.
ejson, err := c.DB.Get(INDEX, h)
if err != nil {
return err
}
var block []string
var pjson []byte
if len(ejson) > 0 {
// Decrypt the blob with k2.
pjson, err = Decrypt(ejson, k2)
if err != nil {
return err
}
err = json.Unmarshal(pjson, &block)
if err != nil {
return err
}
}
// If we are overflowing this block
if len(block) >= 10 {
block = make([]string, 10)
max = max + 1
h = HMAC(append([]byte("COUNT"), byte(max)), k1)
}
block = append(block, doc)
// Get the json for the array
pjson, err = json.Marshal(block)
if err != nil {
return err
}
ejson, err = Encrypt(pjson, k2)
if err != nil {
return err
}
err = c.DB.Put(INDEX, h, ejson)
if err != nil {
return err
}
c.SetCount(keyword, count+1)
return err
}
// Set the key for the client.
func (c *Client) SetKey(passphrase, salt string, iter int) {
c.key = Key([]byte(passphrase), []byte(salt), iter)
}