Skip to content

Commit

Permalink
Merge pull request #1 from codeesura/main
Browse files Browse the repository at this point in the history
test: refactor and format test functions
  • Loading branch information
TomAFrench authored Sep 13, 2024
2 parents 847d50c + 8878ea4 commit 2023620
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
Binary file removed signature_gen/.DS_Store
Binary file not shown.
56 changes: 29 additions & 27 deletions signature_gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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::<Sha256>::new(priv_key);
Expand All @@ -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));

Expand Down Expand Up @@ -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::<Sha256>::new(priv_key);
let sig: Vec<u8> = signing_key.sign(text.as_bytes()).to_vec();
let verifying_key = VerifyingKey::<Sha256>::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::<Sha256>::new(priv_key);
let sig: Vec<u8> = signing_key.sign(text.as_bytes()).to_vec();
let verifying_key = VerifyingKey::<Sha256>::new(pub_key);

let result = verifying_key.verify(
text.as_bytes(),
&Signature::try_from(sig.as_slice()).unwrap(),
);
result.expect("failed to verify");
}
}

0 comments on commit 2023620

Please sign in to comment.