use native_ossl::digest::DigestAlg;
use native_ossl::kdf::HkdfBuilder;
use native_ossl::rand::Rand;
use native_ossl::util::SecretBuf;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut key = SecretBuf::with_len(32);
Rand::fill(key.as_mut_slice())?;
println!(
"Random key ({} bytes): {}",
key.len(),
hex::encode(key.as_ref())
);
let sha256 = DigestAlg::fetch(c"SHA2-256", None)?;
let mut derived = SecretBuf::with_len(32);
HkdfBuilder::new(&sha256)
.key(key.as_ref())
.salt(b"application-specific-salt")
.info(b"encryption key")
.derive(derived.as_mut_slice())?;
println!(
"Derived key ({} bytes): {}",
derived.len(),
hex::encode(derived.as_ref())
);
let raw: Vec<u8> = vec![0x42u8; 16];
let secret = SecretBuf::new(raw);
println!(
"Wrapped allocation ({} bytes): {}",
secret.len(),
hex::encode(secret.as_ref())
);
let source = b"password";
let copy = SecretBuf::from_slice(source);
assert_eq!(copy.as_ref(), source.as_ref());
println!("from_slice copy: {:?}", std::str::from_utf8(copy.as_ref())?);
Ok(())
}