aboutsummaryrefslogtreecommitdiff
path: root/internal/utils/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/utils/utils.go')
-rw-r--r--internal/utils/utils.go35
1 files changed, 34 insertions, 1 deletions
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