dsa 0.7.0

Pure Rust implementation of the Digital Signature Algorithm (DSA) as specified in FIPS 186-4 (Digital Signature Standard), providing RFC6979 deterministic signatures as well as support for added entropy
Documentation
//! Signing example.

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

use digest::Digest;
use dsa::{Components, KeySize, SigningKey};
use getrandom::{SysRng, rand_core::UnwrapErr};
use pkcs8::{EncodePrivateKey, EncodePublicKey, LineEnding};
use sha1::Sha1;
use signature::{RandomizedDigestSigner, SignatureEncoding};
use std::{fs::File, io::Write};

fn main() {
    let components =
        Components::try_generate_from_rng_with_key_size(&mut SysRng, KeySize::DSA_2048_256)
            .unwrap();

    let mut rng = UnwrapErr(SysRng);
    let signing_key =
        SigningKey::try_generate_from_rng_with_components(&mut rng, components).unwrap();
    let verifying_key = signing_key.verifying_key();

    let signature = signing_key
        .sign_digest_with_rng(&mut rng, |digest: &mut Sha1| digest.update(b"hello world"));

    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("signature.der").unwrap();
    file.write_all(&signature.to_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();
}