Skip to content

Commit 8b026ff

Browse files
committed
Include key type in name for type specific function.
1 parent 17adf6a commit 8b026ff

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

src/cryptojwt/jwk/ec.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from cryptography.hazmat.backends import default_backend
2-
from cryptography.hazmat.primitives import serialization
32
from cryptography.hazmat.primitives.asymmetric import ec
43

54
from ..exception import DeSerializationNotPossible
@@ -9,6 +8,7 @@
98
from ..utils import deser
109
from ..utils import long_to_base64
1110
from .asym import AsymmetricKey
11+
from .x509 import import_private_key_from_pem_file
1212
from .x509 import import_public_key_from_pem_data
1313
from .x509 import import_public_key_from_pem_file
1414

@@ -66,38 +66,6 @@ def ec_construct_private(num):
6666
return priv_ecpn.private_key(default_backend())
6767

6868

69-
def import_private_key_from_file(filename, passphrase=None):
70-
"""
71-
Read a private Elliptic Curve key from a PEM file.
72-
73-
:param filename: The name of the file
74-
:param passphrase: A pass phrase to use to unpack the PEM file.
75-
:return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
76-
instance
77-
"""
78-
with open(filename, "rb") as key_file:
79-
private_key = serialization.load_pem_private_key(
80-
key_file.read(), password=passphrase, backend=default_backend()
81-
)
82-
83-
return private_key
84-
85-
86-
def import_public_key_from_file(filename):
87-
"""
88-
Read a public Elliptic Curve key from a PEM file.
89-
90-
:param filename: The name of the file
91-
:return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
92-
instance
93-
"""
94-
with open(filename, "rb") as key_file:
95-
public_key = serialization.load_pem_public_key(
96-
key_file.read(), backend=default_backend()
97-
)
98-
return public_key
99-
100-
10169
class ECKey(AsymmetricKey):
10270
"""
10371
JSON Web key representation of a Elliptic curve key.
@@ -250,7 +218,7 @@ def load(self, filename):
250218
251219
:param filename: File name
252220
"""
253-
return self.load_key(import_private_key_from_file(filename))
221+
return self.load_key(import_private_ec_key_from_file(filename))
254222

255223
def decryption_key(self):
256224
"""
@@ -334,6 +302,22 @@ def import_public_ec_key_from_file(filename):
334302
return ValueError("Not a Elliptic Curve key")
335303

336304

305+
def import_private_ec_key_from_file(filename, passphrase=None):
306+
"""
307+
Read a private Elliptic Curve key from a PEM file.
308+
309+
:param filename: The name of the file
310+
:param passphrase: A pass phrase to use to unpack the PEM file.
311+
:return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
312+
instance
313+
"""
314+
private_key = import_private_key_from_pem_file(filename, passphrase)
315+
if isinstance(private_key, ec.EllipticCurvePrivateKey):
316+
return private_key
317+
else:
318+
return ValueError("Not a private Elliptic Curve key")
319+
320+
337321
def import_ec_key(pem_data):
338322
"""
339323
Extract an Elliptic Curve key from a PEM-encoded X.509 certificate

0 commit comments

Comments
 (0)