This repository was archived by the owner on Apr 20, 2025. It is now read-only.
File tree 3 files changed +31
-0
lines changed
3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,19 @@ Alternatively you can use :py:meth:`rsa.PrivateKey.load_pkcs1` and
40
40
... keydata = privatefile.read()
41
41
>>> privkey = rsa.PrivateKey.load_pkcs1(keydata)
42
42
43
+ As public keys can be derived from private keys it is sufficient to
44
+ have only the private one. The :py:class: `rsa.PrivateKey ` class
45
+ has the dedicated method :py:meth: `rsa.PrivateKey.public_key ` to
46
+ retrieve the corresponding :py:class: `rsa.PublicKey ` from it:
47
+
48
+ >>> import rsa
49
+ >>> with open (' private.pem' , mode = ' rb' ) as privatefile:
50
+ ... keydata = privatefile.read()
51
+ >>> privkey = rsa.PrivateKey.load_pkcs1(keydata)
52
+ >>> pubkey = privkey.public_key()
53
+
54
+
55
+
43
56
44
57
Time to generate a key
45
58
++++++++++++++++++++++
Original file line number Diff line number Diff line change @@ -499,6 +499,18 @@ def blinded_encrypt(self, message: int) -> int:
499
499
encrypted = rsa .core .encrypt_int (blinded , self .d , self .n )
500
500
return self .unblind (encrypted , blindfac_inverse )
501
501
502
+ def public_key (self ) -> PublicKey :
503
+ """Generates the corresponding PublicKey from the PrivateKey.
504
+
505
+ Equivalent to
506
+ >>> pubkey = PublicKey(privkey.n, privkey.e)
507
+
508
+ :returns: the public key that belongs to the private key
509
+ :rtype: PublicKey
510
+ """
511
+
512
+ return PublicKey (self .n , self .e )
513
+
502
514
@classmethod
503
515
def _load_pkcs1_der (cls , keyfile : bytes ) -> "PrivateKey" :
504
516
"""Loads a key in PKCS#1 DER format.
Original file line number Diff line number Diff line change @@ -74,6 +74,12 @@ def getprime(_):
74
74
)
75
75
self .assertEqual (39317 , p )
76
76
self .assertEqual (33107 , q )
77
+
78
+ def test_generate_public_from_private (self ):
79
+ pub , priv = rsa .key .newkeys (16 )
80
+ pub_generated = priv .public_key ()
81
+
82
+ self .assertEqual (pub , pub_generated )
77
83
78
84
79
85
class HashTest (unittest .TestCase ):
You can’t perform that action at this time.
0 commit comments