package main import ( "fmt" "os" "github.com/spf13/cobra" "gitlab.com/whiteboardguy/eno/internal/config" "gitlab.com/whiteboardguy/eno/internal/crud" "gitlab.com/whiteboardguy/eno/internal/crypt" "gitlab.com/whiteboardguy/eno/internal/utils" ) var encryptCmd = &cobra.Command{ Use: "encrypt ", Short: "encrypt a file", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { output, err := cmd.Flags().GetString("output") if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if output == "" { output = args[0] + ".eno" } if _, err := os.Stat(output); err == nil { ok, err := utils.AskUserYesNo("file '"+output+"' already exists. Overwrite?", false) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if !ok { cmd.PrintErrln("Error: write aborted") os.Exit(1) } } fileBytes, err := os.ReadFile(args[0]) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } crypt.TryMlock(fileBytes) defer clear(fileBytes) sshFlag, err := cmd.Flags().GetBool("ssh") if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if sshFlag { if config.Conf.DefaultSSHPubPath == "" { cmd.PrintErrln("Error: SSH not configured. Run 'eno ssh init' first") os.Exit(1) } defaultPubKeyBytes, err := os.ReadFile(config.Conf.DefaultSSHPubPath) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } pubKeys, _, err := crud.PromptRecipientKeys(defaultPubKeyBytes) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if len(pubKeys) == 0 { cmd.PrintErrln("Error: at least one recipient public key is required") os.Exit(1) } encrypted, _, err := crypt.EncryptBytesSSH(fileBytes, pubKeys) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if err := os.WriteFile(output, encrypted, 0600); err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } return } fmt.Printf("Enter a passphrase: ") password, err := utils.TakeSecureUserInput() if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } crypt.TryMlock(password) defer clear(password) encrypted, err := crypt.EncryptBytesPass(fileBytes, password) if err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } if err := os.WriteFile(output, encrypted, 0600); err != nil { cmd.PrintErrln("Error:", err) os.Exit(1) } }, }