-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathloadkey.go
90 lines (76 loc) · 2.02 KB
/
loadkey.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/rivo/tview"
)
func showLoadPassword(parent string, path string) {
const (
windowName = "showLoadPassword"
windowWidth = 40
windowHeight = 6
windowTitle = S_WINDOW_SHOWLOADPASSWORD_TITLE
)
form := tview.NewForm()
form.SetTitle(windowTitle)
form.SetBorder(true)
passwordField := tview.NewInputField().SetLabel("Password").
SetFieldWidth(0).
SetMaskCharacter('*')
passChangedFunc := func(text string) {
// create a master key
masterKeyToLoad := newMasterKey()
err := masterKeyToLoad.load([]byte(passwordField.GetText()), path)
if err == nil {
showSuccessWindow(fmt.Sprintf("Successfully Decrypted Master Key!!!\n%v", path), func() {
masterKey = masterKeyToLoad
masterKey.path = path
refresh()
layoutRoot.RemovePage(windowName)
layoutRoot.RemovePage(parent)
})
}
}
// compute hash on the fly
passwordField.SetChangedFunc(passChangedFunc)
form.AddFormItem(passwordField)
form.SetFocus(0)
layoutRoot.AddPage(windowName, popup(windowWidth, windowHeight, form), true, true)
// test empty pass
passChangedFunc("")
}
func showLoadKeyWindow() {
const (
windowName = "showLoadKeyWindow"
windowWidth = 80
windowHeight = 7
windowTitle = S_WINDOW_SHOW_LOADKEY_TITLE
)
// path input field
path, err := os.Getwd()
if err != nil {
panic(err)
}
form := tview.NewForm()
// input field setting
inputField := tview.NewInputField().
SetLabel("Path: ").
SetText(filepath.Join(path, ".safebox.key")).
SetFieldWidth(0)
form.AddFormItem(inputField)
form.AddButton("Load", func() {
if _, err := os.Stat(inputField.GetText()); os.IsNotExist(err) {
showFailWindow(fmt.Sprintf("File: %v does not exists", inputField.GetText()))
} else {
showLoadPassword(windowName, inputField.GetText())
}
})
form.AddButton("...", func() {
showDirWindow(inputField)
})
form.SetBorder(true)
form.SetTitle(windowTitle)
form.SetFocus(0)
layoutRoot.AddPage(windowName, popup(windowWidth, windowHeight, form), true, true)
}