From 050db499d18c61d90a68096027ac0d5aaa842f57 Mon Sep 17 00:00:00 2001 From: ishaan Date: Sun, 12 Jul 2026 16:41:00 +0530 Subject: add init/new/edit subcommands, config, mlock/cleared sensitive info --- internal/utils/utils.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'internal/utils/utils.go') diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 5e6af2d..621b5a0 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -4,6 +4,9 @@ import ( "bytes" "encoding/gob" "fmt" + "os" + "os/signal" + "syscall" "github.com/charmbracelet/x/term" ) @@ -11,6 +14,10 @@ import ( type FileHeaders struct { Salt [16]byte Nonce [12]byte + + // TODO: `Parts (int)` for the number of encrypted parts the file is divided into. + // TODO: `Version (string)` for the version of .eno file. + // TODO: `EncryptMethod (string)` for the mode of encryption (gpg or passphrase). Maybe even stdout command. } func GetFileHeaders(b []byte) (FileHeaders, error) { @@ -33,8 +40,34 @@ func FileHeadersBytes(headers FileHeaders) ([]byte, error) { return buf.Bytes(), nil } +func TryMlock(b []byte) { + if len(b) == 0 { + return + } + if err := syscall.Mlock(b); err != nil { + fmt.Fprintf(os.Stderr, "mlock warning: %v\n", err) + } +} + func TakeSecureUserInput() ([]byte, error) { - sensInfo, err := term.ReadPassword(0) + fd := os.Stdin.Fd() + + oldState, err := term.GetState(fd) + if err != nil { + return nil, fmt.Errorf("failed to get terminal state: %w", err) + } + + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) + defer signal.Stop(sigCh) + + go func() { + <-sigCh + term.Restore(fd, oldState) + os.Exit(130) + }() + + sensInfo, err := term.ReadPassword(fd) fmt.Println() if err != nil { return nil, err -- cgit v1.3