+88
-17
lines changedFilter options
+88
-17
lines changed Original file line number Diff line number Diff line change
@@ -135,7 +135,7 @@ passphrase. When `encrypt` is set to true in the configuration file, the
135
135
default, unencrypted source files are removed when they are successfully
136
136
encrypted. Use the `--encrypt-keep-src` option to keep them or
137
137
`--no-encrypt-keep-src` to force remove them and override the configuration
138
-
file.
138
+
file. If required, checksum of encrypted files are computed.
139
139
140
140
Encrypted files can be decrypted with the correct passphrase and the
141
141
`--decrypt` option. When `--decrypt` is present on the command line, dumps are
Original file line number Diff line number Diff line change
@@ -80,10 +80,12 @@ func ageDecrypt(src io.Reader, dst io.Writer, password string) error {
80
80
return nil
81
81
}
82
82
83
-
func encryptFile(path string, password string, keep bool) error {
83
+
func encryptFile(path string, password string, keep bool) ([]string, error) {
84
+
encrypted := make([]string, 0)
85
+
84
86
i, err := os.Stat(path)
85
87
if err != nil {
86
-
return err
88
+
return encrypted, err
87
89
}
88
90
89
91
if i.IsDir() {
@@ -103,7 +105,7 @@ func encryptFile(path string, password string, keep bool) error {
103
105
defer src.Close()
104
106
105
107
dstFile := fmt.Sprintf("%s.age", path)
106
-
dst, err := os.Create(dstPath)
108
+
dst, err := os.Create(dstFile)
107
109
if err != nil {
108
110
l.Errorln(err)
109
111
return err
@@ -112,12 +114,14 @@ func encryptFile(path string, password string, keep bool) error {
112
114
113
115
if err := ageEncrypt(src, dst, password); err != nil {
114
116
dst.Close()
115
-
os.Remove(dstPath)
117
+
os.Remove(dstFile)
116
118
return fmt.Errorf("could not encrypt %s: %s", path, err)
117
119
}
118
120
121
+
encrypted = append(encrypted, dstFile)
122
+
119
123
if !keep {
120
-
l.Verboseln("removeing source file:", path)
124
+
l.Verboseln("removing source file:", path)
121
125
src.Close()
122
126
if err := os.Remove(path); err != nil {
123
127
return fmt.Errorf("could not remove %s: %w", path, err)
@@ -128,14 +132,14 @@ func encryptFile(path string, password string, keep bool) error {
128
132
})
129
133
130
134
if err != nil {
131
-
return fmt.Errorf("error walking the path %q: %v", path, err)
135
+
return encrypted, fmt.Errorf("error walking the path %q: %v", path, err)
132
136
}
133
137
} else {
134
138
l.Verboseln("encrypting:", path)
135
139
src, err := os.Open(path)
136
140
if err != nil {
137
141
l.Errorln(err)
138
-
return err
142
+
return encrypted, err
139
143
}
140
144
141
145
defer src.Close()
@@ -144,27 +148,29 @@ func encryptFile(path string, password string, keep bool) error {
144
148
dst, err := os.Create(dstFile)
145
149
if err != nil {
146
150
l.Errorln(err)
147
-
return err
151
+
return encrypted, err
148
152
}
149
153
150
154
defer dst.Close()
151
155
152
156
if err := ageEncrypt(src, dst, password); err != nil {
153
157
dst.Close()
154
158
os.Remove(dstFile)
155
-
return fmt.Errorf("could not encrypt %s: %s", path, err)
159
+
return encrypted, fmt.Errorf("could not encrypt %s: %s", path, err)
156
160
}
157
161
162
+
encrypted = append(encrypted, dstFile)
163
+
158
164
if !keep {
159
-
l.Verboseln("removeing source file:", path)
165
+
l.Verboseln("removing source file:", path)
160
166
src.Close()
161
167
if err := os.Remove(path); err != nil {
162
-
return fmt.Errorf("could not remove %s: %w", path, err)
168
+
return encrypted, fmt.Errorf("could not remove %s: %w", path, err)
163
169
}
164
170
}
165
171
}
166
172
167
-
return nil
173
+
return encrypted, nil
168
174
}
169
175
170
176
func decryptFile(path string, password string) error {
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ func checksumFile(path string, algo string) error {
94
94
l.Verboseln("computing checksum of:", path)
95
95
r, cerr := computeChecksum(path, h)
96
96
if cerr != nil {
97
-
return fmt.Errorf("could not checksum %s: %s", path, err)
97
+
return fmt.Errorf("could not checksum %s: %s", path, cerr)
98
98
}
99
99
fmt.Fprintf(o, "%x *%s\n", r, path)
100
100
}
@@ -115,3 +115,52 @@ func checksumFile(path string, algo string) error {
115
115
}
116
116
return nil
117
117
}
118
+
119
+
func checksumFileList(paths []string, algo string, sumFilePrefix string) error {
120
+
var h hash.Hash
121
+
122
+
switch algo {
123
+
case "none":
124
+
return nil
125
+
case "sha1":
126
+
h = sha1.New()
127
+
case "sha224":
128
+
h = sha256.New224()
129
+
case "sha256":
130
+
h = sha256.New()
131
+
case "sha384":
132
+
h = sha512.New384()
133
+
case "sha512":
134
+
h = sha512.New()
135
+
default:
136
+
return fmt.Errorf("unsupported hash algorithm: %s", algo)
137
+
}
138
+
139
+
sumPath := fmt.Sprintf("%s.%s", sumFilePrefix, algo)
140
+
l.Verbosef("create or use checksum file: %s", sumPath)
141
+
o, err := os.OpenFile(sumPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
142
+
if err != nil {
143
+
return fmt.Errorf("could not open %s: %w", sumPath, err)
144
+
}
145
+
146
+
defer o.Close()
147
+
148
+
failed := false
149
+
for _, path := range paths {
150
+
l.Verboseln("computing checksum of:", path)
151
+
r, err := computeChecksum(path, h)
152
+
if err != nil {
153
+
l.Errorf("could not checksum %s: %s", path, err)
154
+
failed = true
155
+
continue
156
+
}
157
+
158
+
fmt.Fprintf(o, "%x *%s\n", r, path)
159
+
}
160
+
161
+
if failed {
162
+
return fmt.Errorf("computing of checksum failed. Please examine output")
163
+
}
164
+
165
+
return nil
166
+
}
Original file line number Diff line number Diff line change
@@ -205,14 +205,23 @@ func main() {
205
205
}
206
206
207
207
if opts.SumAlgo != "none" {
208
-
if err = checksumFile(file, opts.SumAlgo); err != nil {
208
+
if err := checksumFile(file, opts.SumAlgo); err != nil {
209
209
l.Warnln("checksum failed", err)
210
210
}
211
211
}
212
212
213
213
if opts.Encrypt {
214
-
if err = encryptFile(file, opts.CipherPassphrase, opts.EncryptKeepSrc); err != nil {
214
+
encFiles, err := encryptFile(file, opts.CipherPassphrase, opts.EncryptKeepSrc)
215
+
if err != nil {
215
216
l.Warnln("encryption failed", err)
217
+
continue
218
+
}
219
+
if opts.SumAlgo != "none" {
220
+
for _, encFile := range encFiles {
221
+
if err := checksumFile(encFile, opts.SumAlgo); err != nil {
222
+
l.Warnln("checksum failed", err)
223
+
}
224
+
}
216
225
}
217
226
}
218
227
}
@@ -601,10 +610,17 @@ func (d *dump) dump() error {
601
610
// Encrypt the file
602
611
if d.CipherPassphrase != "" {
603
612
l.Infoln("encrypting", file)
604
-
if err = encryptFile(file, d.CipherPassphrase, d.EncryptKeepSrc); err != nil {
613
+
encFiles, err := encryptFile(file, d.CipherPassphrase, d.EncryptKeepSrc)
614
+
if err != nil {
605
615
return fmt.Errorf("encrypt failed: %s", err)
606
616
607
617
}
618
+
619
+
if d.Options.SumAlgo != "none" && len(encFiles) > 0 {
620
+
if err := checksumFileList(encFiles, d.Options.SumAlgo, fmt.Sprintf("%s.age", file)); err != nil {
621
+
return fmt.Errorf("failed to checksum encrypted files: %w", err)
622
+
}
623
+
}
608
624
}
609
625
610
626
d.ExitCode = 0
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4