diff --git a/signature_gen/.DS_Store b/signature_gen/.DS_Store deleted file mode 100644 index 6d64501..0000000 Binary files a/signature_gen/.DS_Store and /dev/null differ diff --git a/signature_gen/src/main.rs b/signature_gen/src/main.rs index afa0ff9..5f8419c 100644 --- a/signature_gen/src/main.rs +++ b/signature_gen/src/main.rs @@ -1,12 +1,9 @@ use num_bigint::BigUint; use rsa::pkcs1v15::Signature; -use rsa::pkcs1v15::VerifyingKey; use rsa::{RsaPrivateKey, RsaPublicKey}; -use std::env; use toml::Value; -use rand; -use rsa::signature::{SignatureEncoding, Signer, Verifier}; +use rsa::signature::{SignatureEncoding, Signer}; use rsa::traits::PublicKeyParts; use sha2::{Digest, Sha256}; @@ -45,7 +42,8 @@ fn generate_2048_bit_signature_parameters(msg: &str, as_toml: bool, exponent: u3 let mut rng: rand::prelude::ThreadRng = rand::thread_rng(); let bits: usize = 2048; let priv_key: RsaPrivateKey = - RsaPrivateKey::new_with_exp(&mut rng, bits, &BigUint::from(exponent)).expect("failed to generate a key"); + RsaPrivateKey::new_with_exp(&mut rng, bits, &BigUint::from(exponent)) + .expect("failed to generate a key"); let pub_key: RsaPublicKey = priv_key.clone().into(); let signing_key = rsa::pkcs1v15::SigningKey::::new(priv_key); @@ -64,8 +62,6 @@ fn generate_2048_bit_signature_parameters(msg: &str, as_toml: bool, exponent: u3 ); if as_toml { - let hash_toml = toml::to_vec(&hashed_as_bytes).unwrap(); - let sig_limbs = split_into_120_bit_limbs(&sig_uint.clone(), 2048); let signature_toml = Value::Array(format_limbs_as_toml_value(&sig_limbs)); @@ -125,24 +121,30 @@ fn main() { generate_2048_bit_signature_parameters(msg, as_toml, e); } -fn test_signature_generation_impl() { - let mut rng = rand::thread_rng(); - let bits = 2048; - let priv_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key"); - let pub_key: RsaPublicKey = priv_key.clone().into(); - let text: &str = "hello world"; - let signing_key = rsa::pkcs1v15::SigningKey::::new(priv_key); - let sig: Vec = signing_key.sign(text.as_bytes()).to_vec(); - let verifying_key = VerifyingKey::::new(pub_key); - - let result = verifying_key.verify( - text.as_bytes(), - &Signature::try_from(sig.as_slice()).unwrap(), - ); - result.expect("failed to verify"); -} - -#[test] -fn test_signature_generation() { - test_signature_generation_impl(); +#[cfg(test)] +mod tests { + use super::*; + use rand::thread_rng; + use rsa::pkcs1v15::Signature; + use rsa::signature::{Signer, Verifier}; + use rsa::{pkcs1v15::VerifyingKey, RsaPrivateKey, RsaPublicKey}; + use sha2::Sha256; + + #[test] + fn test_signature_generation() { + let mut rng = thread_rng(); + let bits = 2048; + let priv_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key"); + let pub_key: RsaPublicKey = priv_key.clone().into(); + let text: &str = "hello world"; + let signing_key = rsa::pkcs1v15::SigningKey::::new(priv_key); + let sig: Vec = signing_key.sign(text.as_bytes()).to_vec(); + let verifying_key = VerifyingKey::::new(pub_key); + + let result = verifying_key.verify( + text.as_bytes(), + &Signature::try_from(sig.as_slice()).unwrap(), + ); + result.expect("failed to verify"); + } }