diff --git a/demo/go.mod b/demo/go.mod new file mode 100644 index 0000000..f416c8f --- /dev/null +++ b/demo/go.mod @@ -0,0 +1,11 @@ +module github.com/tolvumadur/rwcipher/demo + +go 1.21.5 + +require github.com/tolvumadur/rwcipher v1.0.1 + +require ( + golang.org/x/crypto v0.18.0 // indirect + golang.org/x/sys v0.16.0 // indirect + golang.org/x/term v0.16.0 // indirect +) diff --git a/demo/go.sum b/demo/go.sum new file mode 100644 index 0000000..cc58459 --- /dev/null +++ b/demo/go.sum @@ -0,0 +1,8 @@ +github.com/tolvumadur/rwcipher v1.0.1 h1:BZ1XZ6/9jeT9jfXxa0+JiUwlxP7eq4Y2zz9qY5ouO8E= +github.com/tolvumadur/rwcipher v1.0.1/go.mod h1:QvvqzFDk4YosKdqqvZsPRFAo+RMIvYIxFJ1AycrBnU4= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= +golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= diff --git a/demo/main.go b/demo/main.go new file mode 100644 index 0000000..9263e97 --- /dev/null +++ b/demo/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "flag" + "github.com/tolvumadur/rwcipher" + "fmt" + "os" + "strings" +) + +func init() { + +} + + +func main() { + + // Collect command line input + var dst = flag.String("o", "../test/test1.tmp", "output file") + var src = flag.String("i", "../test/test.txt", "input file") + var dec = flag.Bool("d", false, "set this flag for decrypting") + var enc = flag.Bool("e", false, "set this flag for encrypting") + var silent = flag.Bool("s", false, "set this flag to silence stdout") + + flag.Parse() + + // Validate boolean inputs + if (*enc && *dec) || (!*enc && !*dec) { + os.Stderr.WriteString("Must specify -d xor -e for decryption or encryption\n") + flag.PrintDefaults() + os.Exit(1) + } + + // Report actions unless silent + if !*silent { + if *enc { + fmt.Printf("Encrypting %s to outfile %s\n", *src, *dst) + } else if *dec { + fmt.Printf("Encrypting %s to outfile %s\n", *src, *dst) + } + } + + // Decrypt or Encrypt using rwcipher, report any error + if *dec { + err := rwcipher.Decrypt(*src, *dst, nil) + if err != nil { + if strings.Contains(err.Error(), "message authentication failed") { + os.Stderr.WriteString("Decryption failed either due to a wrong password or altered ciphertext/nonce/salt in the encrypted file.\n") + } else { + os.Stderr.WriteString(fmt.Sprintf("Decryption Failed: %v\n", err)) + } + os.Exit(4) + } + } else if *enc { + err := rwcipher.Encrypt(*src, *dst, nil) + if err != nil { + fmt.Printf("Encryption Failed: %v\n", err) + os.Exit(3) + } + } else { + os.Stderr.WriteString("Unknown operation requested\n") + flag.PrintDefaults() + os.Exit(2) + } + + if !*silent { + fmt.Println("Operation Successful.\n") + } + + os.Exit(0) +} +