//! Digest example — SHA-256 and SHA-3-256 hashing.
//!
//! Run with: cargo run --example digest -p native-ossl

use native_ossl::digest::DigestAlg;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let message = b"The quick brown fox jumps over the lazy dog";

    // ── SHA-256 ───────────────────────────────────────────────────────────────

    let sha256 = DigestAlg::fetch(c"SHA2-256", None)?;
    println!("SHA-256 output length: {} bytes", sha256.output_len());

    let mut ctx = sha256.new_context()?;
    ctx.update(message)?;

    let mut digest = [0u8; 32];
    ctx.finish(&mut digest)?;
    println!("SHA-256: {}", native_ossl::util::hex_encode(digest));
    // d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

    // ── SHA-3-256 ─────────────────────────────────────────────────────────────

    let sha3 = DigestAlg::fetch(c"SHA3-256", None)?;
    let mut ctx3 = sha3.new_context()?;
    ctx3.update(message)?;

    let mut digest3 = [0u8; 32];
    ctx3.finish(&mut digest3)?;
    println!("SHA3-256: {}", native_ossl::util::hex_encode(digest3));

    // ── Streaming (multi-update) ──────────────────────────────────────────────

    let mut ctx2 = sha256.new_context()?;
    ctx2.update(b"The quick brown fox ")?;
    ctx2.update(b"jumps over ")?;
    ctx2.update(b"the lazy dog")?;
    let mut digest2 = [0u8; 32];
    ctx2.finish(&mut digest2)?;
    assert_eq!(digest, digest2, "streaming and one-shot must agree");
    println!("Streaming SHA-256 matches one-shot: OK");

    Ok(())
}
