-
Notifications
You must be signed in to change notification settings - Fork 2
/
decrypt.go
70 lines (56 loc) · 1.69 KB
/
decrypt.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
package main
import (
"errors"
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
)
var decryptCommand *kingpin.CmdClause
var decryptUsername *string
var decryptPrivateKeyFile *string
func setupDecryptCommand(app *kingpin.Application) {
decryptCommand := app.Command("decrypt", "Decrypt the decryptable parts of the file")
decryptUsername = decryptCommand.Flag("user", "Name of user").Short('u').Default(configDefaults[keyUsername]).String()
decryptPrivateKeyFile = decryptCommand.Flag("pvt-key", "Filename of private key").Short('k').Default(configDefaults[keyPrivateKeyFile]).String()
}
func handleDecryptCommand(commands []string) error {
mealieCryptFile, err := readFile(filename, true)
if err != nil {
return err
}
_, found := mealieCryptFile.Users[*decryptUsername]
if !found {
return errors.New(fmt.Sprintf("User not found : %s", *decryptUsername))
}
pvtKey, err := readPrivateKey(*decryptPrivateKeyFile)
if err != nil {
return err
}
for groupName, group := range mealieCryptFile.Groups {
encSymKey, found := group.Keys[*decryptUsername]
if found {
if group.Decrypted == nil {
group.Decrypted = make(map[string]string)
}
symKey, err := decryptSymmetricalKey(encSymKey, pvtKey)
if err != nil {
return err
}
for encValueName, encValue := range group.Values {
valueName, err := decryptValue(symKey, encValueName)
if err != nil {
return err
}
decValue, err := decryptValue(symKey, encValue)
if err != nil {
return err
}
_, found := group.Decrypted[valueName]
if !found {
group.Decrypted[valueName] = decValue
}
}
mealieCryptFile.Groups[groupName] = group
}
}
return writeFile(filename, false, mealieCryptFile)
}