From 86049a1641535f451fdd5a8bf885ecb925adbf1e Mon Sep 17 00:00:00 2001 From: Roland Vet Date: Wed, 21 Feb 2024 16:45:18 +0100 Subject: Refactor ask_for_key to call new decrypt_master_key Signed-off-by: Roland Vet --- src/key.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/key.rs') diff --git a/src/key.rs b/src/key.rs index ae7e2b26..0a7b4a4d 100644 --- a/src/key.rs +++ b/src/key.rs @@ -75,8 +75,19 @@ fn wait_for_key(uuid: &uuid::Uuid) -> anyhow::Result<()> { } } -const BCH_KEY_MAGIC: &str = "bch**key"; fn ask_for_key(sb: &bch_sb_handle) -> anyhow::Result<()> { + let pass = if stdin().is_terminal() { + rpassword::prompt_password("Enter passphrase: ")? + } else { + let mut line = String::new(); + stdin().read_line(&mut line)?; + line + }; + decrypt_master_key(sb, pass) +} + +const BCH_KEY_MAGIC: &str = "bch**key"; +fn decrypt_master_key(sb: &bch_sb_handle, pass: String) -> anyhow::Result<()> { use bch_bindgen::bcachefs::{self, bch2_chacha_encrypt_key, bch_encrypted_key, bch_key}; use byteorder::{LittleEndian, ReadBytesExt}; use std::os::raw::c_char; @@ -88,13 +99,6 @@ fn ask_for_key(sb: &bch_sb_handle) -> anyhow::Result<()> { let bch_key_magic = BCH_KEY_MAGIC.as_bytes().read_u64::().unwrap(); let crypt = sb.sb().crypt().unwrap(); - let pass = if stdin().is_terminal() { - rpassword::prompt_password("Enter passphrase: ")? - } else { - let mut line = String::new(); - stdin().read_line(&mut line)?; - line - }; let pass = std::ffi::CString::new(pass.trim_end())?; // bind to keep the CString alive let mut output: bch_key = unsafe { bcachefs::derive_passphrase( -- cgit v1.2.3