Skip to content

Commit e61c7a1

Browse files
committed
construct keys inline
1 parent 1a129ea commit e61c7a1

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

src/cryptojwt/jwk/okp.py

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,6 @@ def deser(val):
4949
return b64d(_val)
5050

5151

52-
def okp_construct_public(num) -> OKPPublicKey:
53-
"""
54-
Given a set of values on public attributes build a OKP public key instance.
55-
56-
:param num: A dictionary with public attributes and their values
57-
:return: A OKPPublicKey instance.
58-
"""
59-
try:
60-
return CRV2PUBLIC[as_unicode(num["crv"])].from_public_bytes(num["x"])
61-
except KeyError:
62-
raise UnsupportedOKPCurve("Unsupported OKP curve: {}".format(num["crv"]))
63-
64-
65-
def okp_construct_private(num) -> OKPPrivateKey:
66-
"""
67-
Given a set of values on public and private attributes build a elliptic
68-
curve private key instance.
69-
70-
:param num: A dictionary with public and private attributes and their values
71-
:return: A OKPPrivateKey instance.
72-
"""
73-
try:
74-
return CRV2PRIVATE[as_unicode(num["crv"])].from_private_bytes(num["d"])
75-
except KeyError:
76-
raise UnsupportedOKPCurve("Unsupported OKP curve: {}".format(num["crv"]))
77-
78-
7952
class OKPKey(AsymmetricKey):
8053
"""
8154
JSON Web key representation of an Octet Key Pair key.
@@ -147,13 +120,18 @@ def deserialize(self):
147120
if self.d:
148121
try:
149122
if isinstance(self.d, (str, bytes)):
150-
_d = deser(self.d)
151-
self.priv_key = okp_construct_private({"x": _x, "crv": self.crv, "d": _d})
123+
try:
124+
self.priv_key = CRV2PRIVATE[self.crv].from_private_bytes(deser(self.d))
125+
except KeyError:
126+
raise UnsupportedOKPCurve("Unsupported OKP curve: {}".format(self.crv))
152127
self.pub_key = self.priv_key.public_key()
153128
except ValueError as err:
154129
raise DeSerializationNotPossible(str(err))
155130
else:
156-
self.pub_key = okp_construct_public({"x": _x, "crv": self.crv})
131+
try:
132+
self.pub_key = CRV2PUBLIC[self.crv].from_public_bytes(_x)
133+
except KeyError:
134+
raise UnsupportedOKPCurve("Unsupported OKP curve: {}".format(num["crv"]))
157135

158136
def _serialize(self, key):
159137
if isinstance(key, ed25519.Ed25519PublicKey):

0 commit comments

Comments
 (0)