Skip to content

Commit

Permalink
Merge 7addc2f into fed45a6
Browse files Browse the repository at this point in the history
  • Loading branch information
tomato42 authored Nov 30, 2019
2 parents fed45a6 + 7addc2f commit 1a828b5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/ecdsa/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __repr__(self):
pub_key = self.to_string("compressed")
return "VerifyingKey.from_string({0!r}, {1!r}, {2})".format(
pub_key, self.curve, self.default_hashfunc().name)

def __eq__(self, other):
"""Return True if the points are identical, False otherwise."""
if isinstance(other, VerifyingKey):
Expand Down Expand Up @@ -290,7 +290,7 @@ def from_string(cls, string, curve=NIST192p, hashfunc=sha1,
return cls.from_public_point(point, curve, hashfunc)

@classmethod
def from_pem(cls, string):
def from_pem(cls, string, hashfunc=sha1):
"""
Initialise from public key stored in :term:`PEM` format.
Expand All @@ -308,10 +308,10 @@ def from_pem(cls, string):
:return: Initialised VerifyingKey object
:rtype: VerifyingKey
"""
return cls.from_der(der.unpem(string))
return cls.from_der(der.unpem(string), hashfunc=hashfunc)

@classmethod
def from_der(cls, string):
def from_der(cls, string, hashfunc=sha1):
"""
Initialise the key stored in :term:`DER` format.
Expand Down Expand Up @@ -364,7 +364,7 @@ def from_der(cls, string):
# raw encoding of point is invalid in DER files
if len(point_str) == curve.verifying_key_length:
raise der.UnexpectedDER("Malformed encoding of public point")
return cls.from_string(point_str, curve)
return cls.from_string(point_str, curve, hashfunc=hashfunc)

@classmethod
def from_public_key_recovery(cls, signature, data, curve, hashfunc=sha1,
Expand Down
29 changes: 20 additions & 9 deletions src/ecdsa/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ def setUpClass(cls):
"-----BEGIN PUBLIC KEY-----\n"
"MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEuIF30ITvF/XkVjlAgCg2D59ZtKTX\n"
"Jk5i2gZR3OR6NaTFtFz1FZNCOotVe5wgmfNs\n"
"-----END PUBLIC KEY-----\n")

"-----END PUBLIC KEY-----\n")
cls.key_pem = key_str

cls.key_bytes = unpem(key_str)
assert isinstance(cls.key_bytes, bytes)
cls.vk = VerifyingKey.from_pem(key_str)
cls.sk = SigningKey.from_pem(prv_key_str)

key_str = (
"-----BEGIN PUBLIC KEY-----\n"
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE4H3iRbG4TSrsSRb/gusPQB/4YcN8\n"
Expand All @@ -135,6 +136,16 @@ def setUpClass(cls):
)
cls.vk2 = VerifyingKey.from_pem(key_str)

def test_custom_hashfunc(self):
vk = VerifyingKey.from_der(self.key_bytes, hashlib.sha256)

self.assertIs(vk.default_hashfunc, hashlib.sha256)

def test_from_pem_with_custom_hashfunc(self):
vk = VerifyingKey.from_pem(self.key_pem, hashlib.sha256)

self.assertIs(vk.default_hashfunc, hashlib.sha256)

def test_bytes(self):
vk = VerifyingKey.from_der(self.key_bytes)

Expand Down Expand Up @@ -166,14 +177,14 @@ def test_array_array_of_bytes_memoryview(self):
vk = VerifyingKey.from_der(buffer(arr))

self.assertEqual(self.vk.to_string(), vk.to_string())
def test_equality_on_verifying_keys(self):

def test_equality_on_verifying_keys(self):
self.assertEqual(self.vk, self.sk.get_verifying_key())
def test_inequality_on_verifying_keys(self):

def test_inequality_on_verifying_keys(self):
self.assertNotEqual(self.vk, self.vk2)
def test_inequality_on_verifying_keys_not_implemented(self):

def test_inequality_on_verifying_keys_not_implemented(self):
self.assertNotEqual(self.vk, None)


Expand Down

0 comments on commit 1a828b5

Please sign in to comment.