forked from xtaci/safebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen.go
100 lines (85 loc) · 2.53 KB
/
keygen.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
package main
import (
"encoding/hex"
"fmt"
"os"
"github.com/rivo/tview"
)
var keyGenWindowTitle = "- KEY GENERATION -"
func rawOutput(primitive tview.Primitive) *tview.Flex {
return tview.NewFlex().
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(primitive, 1, 1, true).
AddItem(nil, 0, 1, false), 1, 1, true)
}
func showKeyGenPasswordPrompt(parent string, path string) {
windowName := "showKeyGenPasswordPrompt"
form := tview.NewForm()
form.SetBorder(true)
passwordField := tview.NewInputField().SetLabel("Password").
SetFieldWidth(64).
SetMaskCharacter('*')
form.AddFormItem(passwordField)
form.AddButton("OK", func() {
err := masterKey.store([]byte(passwordField.GetText()), path)
// display message after store
if err != nil {
showFailWindow("FAILURE", err.Error())
} else {
masterKey.path = path
showSuccessWindow("SUCCESS", fmt.Sprint("Successfully Stored Master Key!!!\n", path), func() {
info = infoWindow()
mainFrame = mainFrameWindow()
refreshBody()
root.RemovePage(windowName)
root.RemovePage(parent)
})
}
})
form.SetFocus(0)
root.AddPage(windowName, popup(40, 10, form), true, true)
}
func showKeyGenWindow() {
windowName := "showKeyGenWindow"
text := tview.NewTextView().
SetDynamicColors(true).
SetTextAlign(tview.AlignLeft).
SetWrap(false)
// create a master key
masterKey = newMasterKey()
masterKey.generateMasterKey(nil)
fmt.Fprint(text, "GENERATED MASTER KEY:\n\n")
fmt.Fprintf(text, "[darkorange::bl]%v...\n\n", hex.EncodeToString(masterKey.masterKey[:32]))
fmt.Fprint(text, "[darkorange::bu]MAKE SURE YOU BACKUP THIS FILE CORRECTLY\n")
// path input field
path, err := os.Getwd()
if err != nil {
panic(err)
}
form := tview.NewForm()
inputField := tview.NewInputField().
SetLabel("Save To:").
SetText(path + "/.safebox.key").
SetFieldWidth(64)
form.AddFormItem(inputField)
form.AddButton("Save", func() {
// check file existence
if _, err := os.Stat(inputField.GetText()); os.IsNotExist(err) {
showKeyGenPasswordPrompt(windowName, inputField.GetText())
} else {
showFailWindow("FAILURE", "MASTER KEY FILE EXISTS, IF YOU WANT TO OVERWRITE, PLEASE DELETE THIS FILE BY YOURSELF.")
}
})
form.AddButton("...", func() {
showDirWindow(inputField)
})
form.SetFocus(0)
flex := tview.NewFlex()
flex.SetDirection(tview.FlexRow).
SetTitle(keyGenWindowTitle).
SetBorder(true)
flex.AddItem(text, 0, 1, false)
flex.AddItem(form, 0, 1, true)
root.AddPage(windowName, popup(100, 12, flex), true, true)
}