-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchoice.go
91 lines (77 loc) · 1.52 KB
/
choice.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
package main
import (
"fmt"
"os"
"strings"
"github.com/k0kubun/go-ansi"
"github.com/notwithering/memory"
)
func chooseKey() {
chooseKey:
for {
ansi.Printf(eviInfo, "Encryption key:")
ansi.Printf(eviInfo, "[d]etails")
fmt.Print(eviInput)
in, err := line(true)
if err != nil {
ansi.Printf(eviError, err)
os.Exit(1)
}
switch strings.ToLower(in) {
case "d":
fmt.Print("\n")
ansi.Printf(eviInfoPair, "Editor", editor)
ansi.Printf(eviInfoPair, "Encryption", "AES-256-GCM")
ansi.Printf(eviInfoPair, "File", filename)
ansi.Printf(eviInfoPair, "Hashing", "SHA-256")
fmt.Print("\n")
default:
key = hash([]byte(in))
memory.Zero(&in)
if confirmKey {
fmt.Print(eviInput)
in, err := line(true)
if err != nil {
ansi.Printf(eviError, err)
os.Exit(1)
}
confirmedKey := hash([]byte(in))
memory.Zero(&in)
if !keysMatch(key, confirmedKey) {
ansi.Printf(eviError, "keys do not match")
os.Exit(1)
}
}
break chooseKey
}
}
}
func keysMatch(key, confirmedKey []byte) bool {
if len(key) != len(confirmedKey) {
return false
}
for i, b := range key {
if b != confirmedKey[i] {
return false
}
}
return true
}
func removeFile() {
ansi.Printf(eviInfo, "Remove file? [y/N]")
fmt.Print(eviInput)
in, err := line(false)
if err != nil {
ansi.Printf(eviError, err)
os.Exit(1)
}
switch strings.ToLower(in) {
case "y":
if err := os.Remove(filename); err != nil {
ansi.Printf(eviError, err)
os.Exit(1)
}
default:
return
}
}