diff options
author | Roland Vet <RlndVt@protonmail.com> | 2024-02-21 16:45:18 +0100 |
---|---|---|
committer | Roland Vet <RlndVt@protonmail.com> | 2024-02-22 21:12:02 +0100 |
commit | 86049a1641535f451fdd5a8bf885ecb925adbf1e (patch) | |
tree | 0129c35737e2e76b6796ce935b20e029b79d8ec7 | |
parent | 0728677cdc325d3f9ff37f6a665eca13af5e50cc (diff) |
Refactor ask_for_key to call new decrypt_master_key
Signed-off-by: Roland Vet <RlndVt@protonmail.com>
-rw-r--r-- | src/key.rs | 20 |
1 files changed, 12 insertions, 8 deletions
@@ -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::<LittleEndian>().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( |