owned/
owned.rs

1// Copyright 2025 Steven Dee.
2//
3// This project is made available under a BSD-compatible license. See the
4// LICENSE file in the project root for details.
5//
6// The readpassphrase source and header are copyright 2000-2002, 2007, 2010
7// Todd C. Miller.
8
9use readpassphrase_3::{Error, PASSWORD_LEN, RppFlags, readpassphrase, readpassphrase_owned};
10use zeroize::{Zeroize, Zeroizing};
11
12fn main() -> Result<(), Error> {
13    let mut buf = vec![0u8; PASSWORD_LEN];
14    let pass =
15        Zeroizing::new(readpassphrase(c"Password: ", &mut buf, RppFlags::ECHO_ON)?.to_string());
16    let mut buf = Some(buf);
17    loop {
18        let mut res = readpassphrase_owned(
19            c"Confirmation: ",
20            buf.take().unwrap(),
21            RppFlags::REQUIRE_TTY,
22        )?;
23        if *pass == res {
24            res.zeroize();
25            break;
26        }
27        buf = Some(res.into());
28    }
29    Ok(())
30}