use ring::{
    signature::{ KeyPair, Ed25519KeyPair, UnparsedPublicKey, ED25519 },
    hmac, rand, error::Unspecified,
    digest,
};

const SEED_LEN: usize = 32;

fn main() -> Result<(), Unspecified> {
    {
        println!("{} HHmac {}", "-".repeat(10), "-".repeat(10));
        let rng = rand::SystemRandom::new();
        let key = hmac::Key::generate(hmac::HMAC_SHA256, &rng)?;

        let msg = "hello, world";
        let tag = hmac::sign(&key, msg.as_bytes());

        println!("{:?}", tag);
        hmac::verify(&key, msg.as_bytes(), tag.as_ref())?;
        println!("Verify success");
    }
    {
        println!("{} SHA256 {}", "-".repeat(10), "-".repeat(10));
        let sha256 = digest::digest(&digest::SHA256, b"hello, world");
        println!("{:?}", sha256);
    }
    {
        println!("{} EdDSA  {}", "-".repeat(10), "-".repeat(10));
        let rng = rand::SystemRandom::new();
        let seed: [u8; SEED_LEN] = rand::generate(&rng)?.expose();
        println!("seed: {}", hex::encode(&seed));
        let key_pair = Ed25519KeyPair::from_seed_unchecked(&seed)?;
        println!("private: {:?}", key_pair);
        let sig = key_pair.sign(&"hello world".as_bytes());
        println!("signature: {}", hex::encode(sig.as_ref()));
        let public_key = key_pair.public_key().as_ref();
        let verify_result = UnparsedPublicKey::new(&ED25519, &public_key).verify(&"hello world".as_bytes(), sig.as_ref());
        println!("verify: {}", verify_result.is_ok());
    }

    Ok(())
}
