-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tolvumadur
committed
Jan 10, 2024
1 parent
c60c03f
commit 960f2bc
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|