From ee3e75640c98062952be2dd29118745f10e921af Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 16 Feb 2020 10:02:23 +0100 Subject: [PATCH] Add overwrite confirmation check if the output file exists --- cryptpng.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cryptpng.go b/cryptpng.go index e5a700e..19d65e5 100644 --- a/cryptpng.go +++ b/cryptpng.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "crypto/aes" "crypto/cipher" "crypto/rand" @@ -13,6 +14,7 @@ import ( "os" "syscall" "math" + "strings" "golang.org/x/crypto/scrypt" "golang.org/x/crypto/ssh/terminal" @@ -45,7 +47,7 @@ func main() { decryptFlags.StringVar(&imageFile, "image", "image.png", "The path of the png file.") encryptFlags.StringVar(&inputFile, "in", "input.txt","The file with the input data.") encryptFlags.StringVar(&outputFile, "out", "out.png", "The output filename for the image.") - decryptFlags.StringVar(&outputFile, "out", "out.txt", "The output file for the decrypted data.") + decryptFlags.StringVar(&outputFile, "out", "out.png", "The output file for the decrypted data.") flag.Parse() switch os.Args[1] { case "encrypt": @@ -55,6 +57,13 @@ func main() { check(err) defer f.Close() check(err) + if _, err := os.Stat(outputFile); err == nil { + reader := bufio.NewReader(os.Stdin) + fmt.Printf("The output file %s exists and will be overwritten. Continue? [Y/n] ", outputFile) + if ans, _ := reader.ReadString('\n'); strings.ToLower(ans) != "y\n" { + log.Fatal("Aborting...") + } + } fout, err := os.Create(outputFile) check(err) defer fout.Close()