1
1
use num_bigint:: BigUint ;
2
2
use rsa:: pkcs1v15:: Signature ;
3
- use rsa:: pkcs1v15:: VerifyingKey ;
4
3
use rsa:: { RsaPrivateKey , RsaPublicKey } ;
5
- use std:: env;
6
4
use toml:: Value ;
7
5
8
- use rand;
9
- use rsa:: signature:: { SignatureEncoding , Signer , Verifier } ;
6
+ use rsa:: signature:: { SignatureEncoding , Signer } ;
10
7
use rsa:: traits:: PublicKeyParts ;
11
8
use sha2:: { Digest , Sha256 } ;
12
9
@@ -45,7 +42,8 @@ fn generate_2048_bit_signature_parameters(msg: &str, as_toml: bool, exponent: u3
45
42
let mut rng: rand:: prelude:: ThreadRng = rand:: thread_rng ( ) ;
46
43
let bits: usize = 2048 ;
47
44
let priv_key: RsaPrivateKey =
48
- RsaPrivateKey :: new_with_exp ( & mut rng, bits, & BigUint :: from ( exponent) ) . expect ( "failed to generate a key" ) ;
45
+ RsaPrivateKey :: new_with_exp ( & mut rng, bits, & BigUint :: from ( exponent) )
46
+ . expect ( "failed to generate a key" ) ;
49
47
let pub_key: RsaPublicKey = priv_key. clone ( ) . into ( ) ;
50
48
51
49
let signing_key = rsa:: pkcs1v15:: SigningKey :: < Sha256 > :: new ( priv_key) ;
@@ -64,8 +62,6 @@ fn generate_2048_bit_signature_parameters(msg: &str, as_toml: bool, exponent: u3
64
62
) ;
65
63
66
64
if as_toml {
67
- let hash_toml = toml:: to_vec ( & hashed_as_bytes) . unwrap ( ) ;
68
-
69
65
let sig_limbs = split_into_120_bit_limbs ( & sig_uint. clone ( ) , 2048 ) ;
70
66
let signature_toml = Value :: Array ( format_limbs_as_toml_value ( & sig_limbs) ) ;
71
67
@@ -125,24 +121,30 @@ fn main() {
125
121
generate_2048_bit_signature_parameters ( msg, as_toml, e) ;
126
122
}
127
123
128
- fn test_signature_generation_impl ( ) {
129
- let mut rng = rand:: thread_rng ( ) ;
130
- let bits = 2048 ;
131
- let priv_key = RsaPrivateKey :: new ( & mut rng, bits) . expect ( "failed to generate a key" ) ;
132
- let pub_key: RsaPublicKey = priv_key. clone ( ) . into ( ) ;
133
- let text: & str = "hello world" ;
134
- let signing_key = rsa:: pkcs1v15:: SigningKey :: < Sha256 > :: new ( priv_key) ;
135
- let sig: Vec < u8 > = signing_key. sign ( text. as_bytes ( ) ) . to_vec ( ) ;
136
- let verifying_key = VerifyingKey :: < Sha256 > :: new ( pub_key) ;
137
-
138
- let result = verifying_key. verify (
139
- text. as_bytes ( ) ,
140
- & Signature :: try_from ( sig. as_slice ( ) ) . unwrap ( ) ,
141
- ) ;
142
- result. expect ( "failed to verify" ) ;
143
- }
144
-
145
- #[ test]
146
- fn test_signature_generation ( ) {
147
- test_signature_generation_impl ( ) ;
124
+ #[ cfg( test) ]
125
+ mod tests {
126
+ use super :: * ;
127
+ use rand:: thread_rng;
128
+ use rsa:: pkcs1v15:: Signature ;
129
+ use rsa:: signature:: { Signer , Verifier } ;
130
+ use rsa:: { pkcs1v15:: VerifyingKey , RsaPrivateKey , RsaPublicKey } ;
131
+ use sha2:: Sha256 ;
132
+
133
+ #[ test]
134
+ fn test_signature_generation ( ) {
135
+ let mut rng = thread_rng ( ) ;
136
+ let bits = 2048 ;
137
+ let priv_key = RsaPrivateKey :: new ( & mut rng, bits) . expect ( "failed to generate a key" ) ;
138
+ let pub_key: RsaPublicKey = priv_key. clone ( ) . into ( ) ;
139
+ let text: & str = "hello world" ;
140
+ let signing_key = rsa:: pkcs1v15:: SigningKey :: < Sha256 > :: new ( priv_key) ;
141
+ let sig: Vec < u8 > = signing_key. sign ( text. as_bytes ( ) ) . to_vec ( ) ;
142
+ let verifying_key = VerifyingKey :: < Sha256 > :: new ( pub_key) ;
143
+
144
+ let result = verifying_key. verify (
145
+ text. as_bytes ( ) ,
146
+ & Signature :: try_from ( sig. as_slice ( ) ) . unwrap ( ) ,
147
+ ) ;
148
+ result. expect ( "failed to verify" ) ;
149
+ }
148
150
}
0 commit comments