forked from tobischo/gokeepasslib
/
protectedstream.go
78 lines (66 loc) · 2.15 KB
/
protectedstream.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
package gokeepasslib
// ProtectedStreamManager is an interface for the different types of StreamManagers
// which might be used for protecting certain values
type ProtectedStreamManager interface {
Unpack(payload string) []byte
Pack(payload []byte) string
}
// InsecureStreamManager is a stream manger which does not encrypt, just stores the plaintext payload
type InsecureStreamManager struct{}
// Unpack returns the given string as a byte slice without any other action being taken
func (i InsecureStreamManager) Unpack(payload string) []byte {
return []byte(payload)
}
// Pack returns the string belonging to the given byte slice payload without any
// packaging to be done
func (i InsecureStreamManager) Pack(payload []byte) string {
return string(payload)
}
func UnlockProtectedGroups(p ProtectedStreamManager, gs []Group) {
for i, _ := range gs { //For each top level group
UnlockProtectedGroup(p, &gs[i])
}
}
func UnlockProtectedGroup(p ProtectedStreamManager, g *Group) {
UnlockProtectedEntries(p, g.Entries)
UnlockProtectedGroups(p, g.Groups)
}
func UnlockProtectedEntries(p ProtectedStreamManager, e []Entry) {
for i, _ := range e {
UnlockProtectedEntry(p, &e[i])
}
}
func UnlockProtectedEntry(p ProtectedStreamManager, e *Entry) {
for i, _ := range e.Values {
if bool(e.Values[i].Value.Protected) {
e.Values[i].Value.Content = string(p.Unpack(e.Values[i].Value.Content))
}
}
for i, _ := range e.Histories {
UnlockProtectedEntries(p, e.Histories[i].Entries)
}
}
func LockProtectedGroups(p ProtectedStreamManager, gs []Group) {
for i, _ := range gs {
LockProtectedGroup(p, &gs[i])
}
}
func LockProtectedGroup(p ProtectedStreamManager, g *Group) {
LockProtectedEntries(p, g.Entries)
LockProtectedGroups(p, g.Groups)
}
func LockProtectedEntries(p ProtectedStreamManager, es []Entry) {
for i, _ := range es {
LockProtectedEntry(p, &es[i])
}
}
func LockProtectedEntry(p ProtectedStreamManager, e *Entry) {
for i, _ := range e.Values {
if bool(e.Values[i].Value.Protected) {
e.Values[i].Value.Content = p.Pack([]byte(e.Values[i].Value.Content))
}
}
for i, _ := range e.Histories {
UnlockProtectedEntries(p, e.Histories[i].Entries)
}
}