//! Example for serializing keys in SPKI/PKCS#8 format.

#![cfg(feature = "hazmat")]
#![allow(clippy::unwrap_used, reason = "tests")]

use dsa::{Components, KeySize, SigningKey};
use getrandom::SysRng;
use pkcs8::{EncodePrivateKey, EncodePublicKey, LineEnding};
use std::{fs::File, io::Write};

fn main() {
    let mut rng = rand_core::UnwrapErr(SysRng);
    let components =
        Components::try_generate_from_rng_with_key_size(&mut rng, KeySize::DSA_2048_256).unwrap();
    let signing_key =
        SigningKey::try_generate_from_rng_with_components(&mut rng, components).unwrap();
    let verifying_key = signing_key.verifying_key();

    let signing_key_bytes = signing_key.to_pkcs8_pem(LineEnding::LF).unwrap();
    let verifying_key_bytes = verifying_key.to_public_key_pem(LineEnding::LF).unwrap();

    let mut file = File::create("public.pem").unwrap();
    file.write_all(verifying_key_bytes.as_bytes()).unwrap();
    file.flush().unwrap();

    let mut file = File::create("private.pem").unwrap();
    file.write_all(signing_key_bytes.as_bytes()).unwrap();
    file.flush().unwrap();
}
