Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for setting hashfunc on verifying key load #167

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -306,7 +306,7 @@ def from_string(cls, string, curve=NIST192p, hashfunc=sha1,
validate_point)

@classmethod
def from_pem(cls, string):
def from_pem(cls, string, hashfunc=sha1):
"""
Initialise from public key stored in :term:`PEM` format.

Expand All @@ -324,10 +324,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 @@ -380,7 +380,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