Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor and format test functions #1

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
Loading