1
+ use pkcs1:: DecodeRsaPrivateKey ;
2
+ use rsa:: RsaPrivateKey ;
3
+ use signature:: { Keypair , Signer , Verifier } ;
4
+
1
5
// simple but prevent regression - see https://github.com/RustCrypto/RSA/issues/329
2
6
#[ cfg( feature = "pem" ) ]
3
7
#[ test]
@@ -21,3 +25,55 @@ fn signature_stringify() {
21
25
assert_eq ! ( format!( "{:X}" , signature) , expected) ;
22
26
assert_eq ! ( signature. to_string( ) , expected) ;
23
27
}
28
+
29
+ #[ test]
30
+ fn signing_key_new_same_as_from ( ) {
31
+ let priv_key = RsaPrivateKey :: from_pkcs1_pem ( PRIV_KEY_PKCS1_PEM ) . unwrap ( ) ;
32
+
33
+ let msg = b"1234" ;
34
+
35
+ let key_via_new = rsa:: pkcs1v15:: SigningKey :: < sha2:: Sha256 > :: new ( priv_key. clone ( ) ) ;
36
+ let key_via_from = rsa:: pkcs1v15:: SigningKey :: < sha2:: Sha256 > :: from ( priv_key. clone ( ) ) ;
37
+ let sig_via_new = key_via_new. sign ( msg) ;
38
+ let sig_via_from = key_via_from. sign ( msg) ;
39
+ assert_eq ! ( sig_via_new, sig_via_from) ;
40
+
41
+ // each verifies the other
42
+ assert ! ( key_via_new
43
+ . verifying_key( )
44
+ . verify( msg, & sig_via_from)
45
+ . is_ok( ) ) ;
46
+ assert ! ( key_via_from
47
+ . verifying_key( )
48
+ . verify( msg, & sig_via_new)
49
+ . is_ok( ) ) ;
50
+ }
51
+
52
+ // randomly generated key, hardcoded for test repeatability
53
+ const PRIV_KEY_PKCS1_PEM : & str = "-----BEGIN RSA PRIVATE KEY-----
54
+ MIIEowIBAAKCAQEAwQe5brkkpxrwR/5TJ6JXsUyBzYtbEL/w8u8P6NnxQ8sL4KYp
55
+ MzzTB6aq1gq7bieYXChg0PIWeTukGaOzZe96KxhT0GbhhYRlukktM/quRrM7nYdm
56
+ UmXo7+KWU55kfcNOjWKADL/7qmxn6y/+kPmBg83nHdr1Mq6/pNkeHY/1CeGGECl0
57
+ rg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