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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package crud
import (
"bytes"
"fmt"
"os"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"gitlab.com/whiteboardguy/eno/internal/editor"
"gitlab.com/whiteboardguy/eno/internal/utils"
)
func NewNotePass(output string) error {
fmt.Printf("Enter the passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
return fmt.Errorf("failed to read passphrase: %v", err)
}
crypt.TryMlock(password)
defer clear(password)
fmt.Printf("Confirm the passphrase: ")
passwordConfirm, err := utils.TakeSecureUserInput()
if err != nil {
return fmt.Errorf("failed to read passphrase: %v", err)
}
crypt.TryMlock(passwordConfirm)
defer clear(passwordConfirm)
if !bytes.Equal(password, passwordConfirm) {
return fmt.Errorf("passphrases do not match")
}
encrypted, err := crypt.EncryptBytesPass(
append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...),
password,
)
if err != nil {
return err
}
fmt.Println(output)
file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(encrypted)
if err != nil {
return err
}
file.Sync()
// utils.ClearTerm()
fmt.Println("New note saved at:", file.Name())
return nil
}
func NewNotePassEdit(output, editorString string) error {
fmt.Printf("Enter the passphrase: ")
password, err := utils.TakeSecureUserInput()
if err != nil {
return fmt.Errorf("failed to read passphrase: %v", err)
}
crypt.TryMlock(password)
defer clear(password)
fmt.Printf("Confirm the passphrase: ")
passwordConfirm, err := utils.TakeSecureUserInput()
if err != nil {
return fmt.Errorf("failed to read passphrase: %v", err)
}
crypt.TryMlock(passwordConfirm)
defer clear(passwordConfirm)
if !bytes.Equal(password, passwordConfirm) {
return fmt.Errorf("passphrases do not match")
}
noteInfo, err := os.Stat(output)
preExisting := err == nil
initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...)
encrypted, err := crypt.EncryptBytesPass(initialContent, password)
if err != nil {
return err
}
file, err := os.OpenFile(output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
_, err = file.Write(encrypted)
if err != nil {
return err
}
file.Sync()
file.Close()
ciphertext, err := os.ReadFile(output)
if err != nil {
return err
}
tmpFile, err := utils.SecureTempFile()
if err != nil {
return err
}
plaintext, err := crypt.DecryptBytesPass(ciphertext, password)
if err != nil {
return err
}
crypt.TryMlock(plaintext)
defer clear(plaintext)
defer deferredCleanTmpFile(tmpFile, len(plaintext))
defer os.Remove(tmpFile.Name())
_, err = tmpFile.Write(plaintext)
if err != nil {
return err
}
err = editor.OpenWithEditor(tmpFile.Name(), editorString)
if err != nil {
return err
}
newContent, err := os.ReadFile(tmpFile.Name())
if err != nil {
return err
}
crypt.TryMlock(newContent)
defer clear(newContent)
if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err)
}
encrypted, err = crypt.EncryptBytesPass(newContent, password)
if err != nil {
return err
}
perm := os.FileMode(0600)
if preExisting {
perm = noteInfo.Mode().Perm()
}
err = os.WriteFile(output, encrypted, perm)
fmt.Println("New note saved at:", output)
return err
}
|