Skip to content

Commit

Permalink
fix read/write mode for various key encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKueltz committed Nov 19, 2020
1 parent e7080d1 commit a67c5b5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 29 deletions.
4 changes: 3 additions & 1 deletion fastecdsa/encoding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
class KeyEncoder:
"""Base class that any encoding class for EC keys should derive from.
All overriding methods should be static.
All overriding methods should be static. If your key encoder writes binary
data you must have a field named :code:`binary_data` set to :code:`True` in
order for keys to correctly read from and write to disk.
"""
__metaclass__ = ABCMeta

Expand Down
1 change: 1 addition & 0 deletions fastecdsa/encoding/pem.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

class PEMEncoder(KeyEncoder):
ASN1_PARSED_DATA = []
binary_data = False

@staticmethod
def _parse_ascii_armored_base64(data: str) -> bytes:
Expand Down
2 changes: 2 additions & 0 deletions fastecdsa/encoding/sec1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class InvalidSEC1PublicKey(Exception):


class SEC1Encoder(KeyEncoder):
binary_data = True

@staticmethod
def encode_public_key(point: Point, compressed: bool = True) -> bytes:
""" Encode a public key as described in http://www.secg.org/SEC1-Ver-1.0.pdf
Expand Down
11 changes: 7 additions & 4 deletions fastecdsa/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ def export_key(key, curve: Curve = None, filepath: str = None, encoder=PEMEncode
if filepath is None:
return encoded
else:
f = open(filepath, 'wb')
f.write(encoded)
f.close()
# some encoder output strings, others bytes, need to determine what mode to write in
write_mode = 'w' + ('b' if getattr(encoder, 'binary_data', False) else '')
with open(filepath, write_mode) as f:
f.write(encoded)


def import_key(filepath: str, curve: Curve = None, public: bool = False, decoder=PEMEncoder
Expand All @@ -161,7 +162,9 @@ def import_key(filepath: str, curve: Curve = None, public: bool = False, decoder
(long, fastecdsa.point.Point): A (private key, public key) tuple. If a public key was
imported then the first value will be None.
"""
with open(filepath, 'r') as f:
# some decoders read strings, others bytes, need to determine what mode to write in
read_mode = 'r' + ('b' if getattr(decoder, 'binary_data', False) else '')
with open(filepath, read_mode) as f:
data = f.read()

if public:
Expand Down
23 changes: 0 additions & 23 deletions fastecdsa/tests/encoding/test_asn1.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def test_export_import_public_key(self):
for encoder in (PEMEncoder, SEC1Encoder):
d, Q = gen_keypair(curve)
export_key(Q, curve=curve, filepath=TEST_FILE_PATH, encoder=encoder)
_, Q_ = import_key(filepath=TEST_FILE_PATH, curve=curve, public=True, decoder=encoder)
d_, Q_ = import_key(filepath=TEST_FILE_PATH, curve=curve, public=True, decoder=encoder)

self.assertIsNone(d_)
self.assertEqual(Q, Q_)

remove(TEST_FILE_PATH)

0 comments on commit a67c5b5

Please sign in to comment.