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::{readpassphrase, readpassphrase_owned, Error, Flags, PASSWORD_LEN};
10use zeroize::{Zeroize, Zeroizing};
11
12fn main() -> Result<(), Error> {
13    let mut buf = Zeroizing::new(Some(vec![0u8; PASSWORD_LEN]));
14    let pass = Zeroizing::new(
15        readpassphrase(c"Password: ", buf.as_deref_mut().unwrap(), Flags::ECHO_ON)?.to_string(),
16    );
17    let mut buf = buf.take();
18    loop {
19        let mut res =
20            readpassphrase_owned(c"Confirmation: ", buf.take().unwrap(), Flags::REQUIRE_TTY)?;
21        if *pass == res {
22            res.zeroize();
23            break;
24        }
25        buf = Some(res.into_bytes());
26    }
27    Ok(())
28}