Skip to content

Commit

Permalink
Erase input file after encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
cynix committed Jun 18, 2023
1 parent 545a6dd commit 92d7deb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ func doAll(files []string, decrypt bool) []string {
}

for _, f := range files {
i, err := os.Open(f)
var i *os.File

if decrypt {
i, err = os.Open(f)
} else {
i, err = os.OpenFile(f, os.O_RDWR, 0)
}
if err != nil {
fmt.Printf("Failed to open: %v\n", err)
failed = append(failed, f)
Expand Down Expand Up @@ -162,6 +168,12 @@ func doAll(files []string, decrypt bool) []string {
}

fmt.Fprintf(os.Stderr, "Encrypted %s\n", f)

if _, err = i.Seek(0, 0); err != nil {
fmt.Fprintf(os.Stderr, "Failed to erase %s: %v\n", f, err)
} else if _, err = io.CopyN(i, ZeroReader, s.Size()); err != nil {
fmt.Fprintf(os.Stderr, "Failed to erase %s: %v\n", f, err)
}
}

if err := os.Remove(f); err != nil {
Expand Down
15 changes: 15 additions & 0 deletions zero.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "io"

var ZeroReader io.Reader = zeroReader{}

type zeroReader struct{}

func (zeroReader) Read(p []byte) (int, error) {
for i := range p {
p[i] = 0
}

return len(p), nil
}

0 comments on commit 92d7deb

Please sign in to comment.