Make adjustments for easier CGO integration

This commit is contained in:
2026-01-21 18:35:26 -05:00
parent f82b35bc43
commit 48e084ac0f
7 changed files with 49 additions and 37 deletions
+35 -26
View File
@@ -13,7 +13,6 @@ import (
"github.com/rwinkhart/rcw/wrappers"
)
var Daemonize = true
var RetryPassword = true
// RCWDArgument reads the password from stdin and caches it via an RCW daemon.
@@ -25,23 +24,29 @@ func RCWDArgument() {
daemon.Start([]byte(password))
}
// DecryptFileToSlice decrypts an RCW wrapped file and returns the contents as a slice of (trimmed) strings.
func DecryptFileToSlice(realPath string) ([]string, error) {
// DecryptFileToSlice decrypts an RCW wrapped file
// and returns the contents as a slice of (trimmed) strings.
// Leave rcwPassword nil to use RCW demonization.
func DecryptFileToSlice(realPath string, rcwPassword []byte) ([]string, error) {
// read encrypted file
encBytes, err := os.ReadFile(realPath)
if err != nil {
return nil, errors.New("unable to open \"" + realPath + "\" for decryption: " + err.Error())
}
// decrypt data using RCW daemon
password := launchRCWDProcess()
if password == nil {
// if daemon is already running, use it to decrypt the data
return strings.Split(string(daemon.GetDec(encBytes)), "\n"), nil
// if no password was provided, assume daemon mode
if rcwPassword == nil {
rcwPassword = launchRCWDProcess()
// if rcwPassword is still nil, the daemon is already running;
// use it to encrypt the data
if rcwPassword == nil {
return strings.Split(string(daemon.GetDec(encBytes)), "\n"), nil
}
}
// if the daemon is not already running, use wrappers.Decrypt
// directly to avoid waiting for socket file creation
decBytes, err := wrappers.Decrypt(encBytes, password)
// if the daemon is not being used/was not already running,
// use wrappers.Decrypt directly to avoid waiting for socket file creation
decBytes, err := wrappers.Decrypt(encBytes, rcwPassword)
if err != nil {
return nil, errors.New("unable to decrypt \"" + realPath + "\": " + err.Error())
}
@@ -49,22 +54,28 @@ func DecryptFileToSlice(realPath string) ([]string, error) {
}
// EncryptBytes encrypts a byte slice using RCW and returns the encrypted data.
func EncryptBytes(decBytes []byte) []byte {
password := launchRCWDProcess()
if password == nil {
// if daemon is already running, use it to encrypt the data
return daemon.GetEnc(decBytes)
// Leave rcwPassword nil to use RCW demonization.
func EncryptBytes(decBytes, rcwPassword []byte) []byte {
// if no rcwPassword was provided, assume daemon mode
if rcwPassword == nil {
rcwPassword = launchRCWDProcess()
// if rcwPassword is still nil, the daemon is already running;
// use it to encrypt the data
if rcwPassword == nil {
return daemon.GetEnc(decBytes)
}
}
// if the daemon is not already running, use wrappers.Encrypt
// directly to avoid waiting for socket file creation
return wrappers.Encrypt(decBytes, password)
// if the daemon is not being used/was not already running,
// use wrappers.Encrypt directly to avoid waiting for socket file creation
return wrappers.Encrypt(decBytes, rcwPassword)
}
// launchRCWDProcess launches an RCW daemon to cache a password.
// If the daemon is not already running OR if not running in daemonize mode,
// it collects and returns the password (otherwise returns nil).
func launchRCWDProcess() []byte {
if Daemonize && daemon.IsOpen() {
if daemon.IsOpen() {
return nil
}
var password []byte
@@ -81,12 +92,10 @@ func launchRCWDProcess() []byte {
password = global.GetPassword("RCW Password:")
}
if Daemonize {
cmd := exec.Command(os.Args[0], "startrcwd")
cmd.SysProcAttr = global.GetSysProcAttr()
_ = back.WriteToStdin(cmd, string(password))
_ = cmd.Start()
}
cmd := exec.Command(os.Args[0], "startrcwd")
cmd.SysProcAttr = global.GetSysProcAttr()
_ = back.WriteToStdin(cmd, string(password))
_ = cmd.Start()
return password
}