forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencryption_utils.go
More file actions
54 lines (43 loc) · 1.19 KB
/
encryption_utils.go
File metadata and controls
54 lines (43 loc) · 1.19 KB
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
package database
import (
"context"
"github.com/sourcegraph/sourcegraph/internal/encryption"
)
type Encrypted struct {
Values []string
KeyID string
}
func encryptValues(ctx context.Context, key encryption.Key, m map[int][]string) (map[int]Encrypted, error) {
encryptedMap := make(map[int]Encrypted, len(m))
for id, vs := range m {
var (
keyID string
encryptedValues = make([]string, 0, len(vs))
)
for _, v := range vs {
ev, id, err := encryption.MaybeEncrypt(ctx, key, v)
if err != nil {
return nil, err
}
keyID = id
encryptedValues = append(encryptedValues, ev)
}
encryptedMap[id] = Encrypted{Values: encryptedValues, KeyID: keyID}
}
return encryptedMap, nil
}
func decryptValues(ctx context.Context, key encryption.Key, m map[int]Encrypted) (map[int][]string, error) {
decryptedMap := make(map[int][]string, len(m))
for id, ev := range m {
decryptedValues := make([]string, 0, len(ev.Values))
for _, v := range ev.Values {
dv, err := encryption.MaybeDecrypt(ctx, key, v, ev.KeyID)
if err != nil {
return nil, err
}
decryptedValues = append(decryptedValues, dv)
}
decryptedMap[id] = decryptedValues
}
return decryptedMap, nil
}