Implement sanity check in example program

This commit is contained in:
2025-05-03 20:05:02 -04:00
parent baad84563e
commit ec358c6d0c
3 changed files with 22 additions and 4 deletions
+2
View File
@@ -2,3 +2,5 @@
/main.exe
/rcw
/rcw.exe
/ex-cipher.rcw
/ex-sanity.rcw
+17 -2
View File
@@ -11,6 +11,7 @@ import (
// of RCW before building it into your own application.
//
// Usage:
// rcw init <passwd> : Generates the required sanity check file
// rcw <text> : Runs the rcw daemon to serve the provided text for three minutes
// rcw : Requests the data served by the RCW daemon and outputs it to stdout
// rcw enc <text> <passwd> : Encrypts the provided text and outputs the ciphertext to encrypted-example.txt
@@ -35,8 +36,17 @@ func main() {
// serve data
daemon.Start(os.Args[1])
case 3:
if os.Args[1] == "init" {
// create sanity check file
err := wrappers.GenSanityCheck("ex-sanity.rcw", []byte(os.Args[2]))
if err != nil {
fmt.Println(err)
}
return
}
// decrypt file
encBytes, _ := os.ReadFile("encrypted-example.txt")
encBytes, _ := os.ReadFile("ex-cipher.rcw")
decBytes, err := wrappers.Decrypt(encBytes, []byte(os.Args[2]))
if err != nil {
fmt.Println(err)
@@ -45,8 +55,13 @@ func main() {
fmt.Println(string(decBytes))
case 4:
// encrypt data (from cli args)
err := wrappers.RunSanityCheck("ex-sanity.rcw", []byte(os.Args[3]))
if err != nil {
fmt.Println(err)
return
}
encBytes := wrappers.Encrypt([]byte(os.Args[2]), []byte(os.Args[3]))
os.WriteFile("encrypted-example.txt", encBytes, 0644)
os.WriteFile("ex-cipher.rcw", encBytes, 0600)
default:
// request served data
fmt.Println(daemon.Call())
+3 -2
View File
@@ -8,8 +8,9 @@ import (
// GenSanityCheck creates an encrypted file containing known plaintext
// to later be used for ensuring the user does not encrypt data with
// an incorrect passphrase.
func GenSanityCheck(path string, passphrase []byte) {
os.WriteFile(path, Encrypt([]byte("thx4usin'rcw"), passphrase), 0600)
func GenSanityCheck(path string, passphrase []byte) error {
err := os.WriteFile(path, Encrypt([]byte("thx4usin'rcw"), passphrase), 0600)
return err
}
// RunSanityCheck should be run before any encryption operation