From baad84563eb4f6c5ecf9460e261358c58515ad26 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 3 May 2025 19:36:23 -0400 Subject: [PATCH] Add sanity check to prevent data loss in the event the user mistypes their passphrase during encryption --- wrappers/sanityCheck.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 wrappers/sanityCheck.go diff --git a/wrappers/sanityCheck.go b/wrappers/sanityCheck.go new file mode 100644 index 0000000..82eadb5 --- /dev/null +++ b/wrappers/sanityCheck.go @@ -0,0 +1,28 @@ +package wrappers + +import ( + "errors" + "os" +) + +// 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) +} + +// RunSanityCheck should be run before any encryption operation +// to ensure the user does not encrypt data with an incorrect passphrase. +// Failure to perform this check could result in data loss. +func RunSanityCheck(path string, passphrase []byte) error { + encBytes, err := os.ReadFile(path) + if err != nil { + return errors.New("Failed to read sanity check file (" + path + ")") + } + decBytes, err := Decrypt(encBytes, passphrase) + if string(decBytes) == "thx4usin'rcw" { + return nil + } + return errors.New("Sanity check failed (likely due to inconsistent passphrase)") +}