mirror of
https://github.com/rwinkhart/rcw.git
synced 2026-08-28 04:46:42 -04:00
33 lines
1.0 KiB
Go
33 lines
1.0 KiB
Go
package wrappers
|
|
|
|
import (
|
|
"bytes"
|
|
"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 password.
|
|
func GenSanityCheck(path string, password []byte, zeroizePassword bool) error {
|
|
err := os.WriteFile(path, Encrypt([]byte("thx4usin'rcw"), password, false, zeroizePassword), 0600)
|
|
return err
|
|
}
|
|
|
|
// RunSanityCheck should be run before any encryption operation
|
|
// to ensure the user does not encrypt data with an incorrect password.
|
|
// Failure to perform this check could result in data loss.
|
|
func RunSanityCheck(path string, password []byte) error {
|
|
encBytes, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return errors.New("unable to read sanity check file (" + path + "): " + err.Error())
|
|
}
|
|
decBytes, err := Decrypt(encBytes, password, false)
|
|
if err == nil {
|
|
if bytes.Equal(decBytes, []byte("thx4usin'rcw")) {
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("sanity check failed (likely due to inconsistent password)")
|
|
}
|