feat: v1.7.3, supports pinentry
This commit is contained in:
19
src/util.rs
19
src/util.rs
@@ -4,12 +4,15 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose;
|
||||
use pinentry::PassphraseInput;
|
||||
use rand::random;
|
||||
use rust_util::{information, opt_result, print_ex, simple_error, util_term, warning, XResult};
|
||||
use secrecy::ExposeSecret;
|
||||
use zeroize::Zeroize;
|
||||
|
||||
use crate::consts::TINY_ENC_FILE_EXT;
|
||||
use crate::util_digest::DigestWrite;
|
||||
use crate::util_env;
|
||||
|
||||
pub struct SecVec(pub Vec<u8>);
|
||||
|
||||
@@ -28,10 +31,22 @@ impl AsRef<[u8]> for SecVec {
|
||||
pub fn read_pin(pin: &Option<String>) -> String {
|
||||
match pin {
|
||||
Some(pin) => pin.to_string(),
|
||||
None => if util_term::read_yes_no("Use default PIN 123456, please confirm") {
|
||||
None => if !util_env::get_no_default_pin_hint() && util_term::read_yes_no("Use default PIN 123456, please confirm") {
|
||||
"123456".into()
|
||||
} else {
|
||||
rpassword::prompt_password("Please input PIN: ").expect("Read PIN failed")
|
||||
let pin_entry = util_env::get_pin_entry().unwrap_or_else(|| "pinentry".to_string());
|
||||
if let Some(mut input) = PassphraseInput::with_binary(pin_entry) {
|
||||
let secret = input
|
||||
.with_description("Enter new passphrase for FooBar")
|
||||
.with_prompt("Passphrase:")
|
||||
.with_confirmation("Confirm passphrase:", "Passphrases do not match")
|
||||
.interact();
|
||||
secret.expect("Read PIN from pinentry failed")
|
||||
.expose_secret()
|
||||
.to_string()
|
||||
} else {
|
||||
rpassword::prompt_password("Please input PIN: ").expect("Read PIN failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ pub const TINY_ENCRYPT_ENV_PIN: &str = "TINY_ENCRYPT_PIN";
|
||||
pub const TINY_ENCRYPT_ENV_KEY_ID: &str = "TINY_ENCRYPT_KEY_ID";
|
||||
pub const TINY_ENCRYPT_ENV_AUTO_SELECT_KEY_IDS: &str = "TINY_ENCRYPT_AUTO_SELECT_KEY_IDS";
|
||||
pub const TINY_ENCRYPT_ENV_GPG_COMMAND: &str = "TINY_ENCRYPT_GPG_COMMAND";
|
||||
pub const TINY_ENCRYPT_ENV_NO_DEFAULT_PIN_HINT: &str = "TINY_ENCRYPT_NO_DEFAULT_PIN_HINT";
|
||||
pub const TINY_ENCRYPT_ENV_PIN_ENTRY: &str = "TINY_ENCRYPT_PIN_ENTRY";
|
||||
|
||||
pub fn get_default_encryption_algorithm() -> Option<&'static str> {
|
||||
let env_default_algorithm = env::var(TINY_ENCRYPT_ENV_DEFAULT_ALGORITHM).ok();
|
||||
@@ -39,6 +41,10 @@ pub fn get_gpg_cmd() -> Option<String> {
|
||||
env::var(TINY_ENCRYPT_ENV_GPG_COMMAND).ok()
|
||||
}
|
||||
|
||||
pub fn get_pin_entry() -> Option<String> {
|
||||
env::var(TINY_ENCRYPT_ENV_PIN_ENTRY).ok()
|
||||
}
|
||||
|
||||
pub fn get_auto_select_key_ids() -> Option<Vec<String>> {
|
||||
env::var(TINY_ENCRYPT_ENV_AUTO_SELECT_KEY_IDS).ok().map(|key_ids| {
|
||||
key_ids.split(',').map(ToString::to_string).collect::<Vec<_>>()
|
||||
@@ -51,4 +57,8 @@ pub fn get_default_compress() -> Option<bool> {
|
||||
|
||||
pub fn get_no_progress() -> bool {
|
||||
rust_util_env::is_env_on(TINY_ENCRYPT_ENV_NO_PROGRESS)
|
||||
}
|
||||
|
||||
pub fn get_no_default_pin_hint() -> bool {
|
||||
rust_util_env::is_env_on(TINY_ENCRYPT_ENV_NO_DEFAULT_PIN_HINT)
|
||||
}
|
||||
Reference in New Issue
Block a user