Skip to content

Commit f84672e

Browse files
author
Roland Hedberg
committed
Merge pull request #41 from rebeckag/rsa_key_private_serialization
Private RSA key serialization
2 parents a326007 + 5f0ba8c commit f84672e

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/jwkest/jwk.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def __init__(self, kty="", alg="", use="", kid="", key=None, x5c=None,
222222
self.inactive_since = 0
223223

224224
def to_dict(self):
225-
res = self.serialize()
225+
res = self.serialize(private=True)
226226
res.update(self.extra_args)
227227
return res
228228

@@ -384,10 +384,18 @@ def serialize(self, private=False):
384384
raise SerializationNotPossible()
385385

386386
res = self.common()
387-
for param in self.longs:
387+
388+
public_longs = list(set(self.public_members) & set(self.longs))
389+
for param in public_longs:
388390
item = getattr(self, param)
389391
if item:
390392
res[param] = long_to_base64(item)
393+
394+
if private:
395+
for param in self.longs:
396+
item = getattr(self, param)
397+
if item:
398+
res[param] = long_to_base64(item)
391399
return res
392400

393401
def _split(self):
@@ -479,7 +487,7 @@ def deserialize(self):
479487
self.curve = NISTEllipticCurve.by_name(self.crv)
480488
if self.d:
481489
try:
482-
if isinstance(self.d, str):
490+
if isinstance(self.d, six.string_types):
483491
self.d = deser(self.d)
484492
except ValueError as err:
485493
raise DeSerializationNotPossible(str(err))

tests/test_2_jwk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_serialize_rsa_priv_key():
206206
def test_import_export_eckey():
207207
_key = ECKey(**ECKEY)
208208
_key.deserialize()
209-
assert _eq(list(_key.keys()), ["y", "x", "crv", "kty"])
209+
assert _eq(list(_key.keys()), ["y", "x", "d", "crv", "kty"])
210210

211211

212212
def test_create_eckey():

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ commands=py.test tests/
1111
deps=pytest
1212
pycrypto
1313
requests
14+
15+
[pep8]
16+
max-line-length=100

0 commit comments

Comments
 (0)