Skip to content

Commit 7cf99f0

Browse files
authored
Merge pull request #167 from tomato42/hash_for_pem
support for setting hashfunc on verifying key load
2 parents fc5cb54 + d5e75ca commit 7cf99f0

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/ecdsa/keys.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def __repr__(self):
139139
pub_key = self.to_string("compressed")
140140
return "VerifyingKey.from_string({0!r}, {1!r}, {2})".format(
141141
pub_key, self.curve, self.default_hashfunc().name)
142-
142+
143143
def __eq__(self, other):
144144
"""Return True if the points are identical, False otherwise."""
145145
if isinstance(other, VerifyingKey):
@@ -306,7 +306,7 @@ def from_string(cls, string, curve=NIST192p, hashfunc=sha1,
306306
validate_point)
307307

308308
@classmethod
309-
def from_pem(cls, string):
309+
def from_pem(cls, string, hashfunc=sha1):
310310
"""
311311
Initialise from public key stored in :term:`PEM` format.
312312
@@ -324,10 +324,10 @@ def from_pem(cls, string):
324324
:return: Initialised VerifyingKey object
325325
:rtype: VerifyingKey
326326
"""
327-
return cls.from_der(der.unpem(string))
327+
return cls.from_der(der.unpem(string), hashfunc=hashfunc)
328328

329329
@classmethod
330-
def from_der(cls, string):
330+
def from_der(cls, string, hashfunc=sha1):
331331
"""
332332
Initialise the key stored in :term:`DER` format.
333333
@@ -380,7 +380,7 @@ def from_der(cls, string):
380380
# raw encoding of point is invalid in DER files
381381
if len(point_str) == curve.verifying_key_length:
382382
raise der.UnexpectedDER("Malformed encoding of public point")
383-
return cls.from_string(point_str, curve)
383+
return cls.from_string(point_str, curve, hashfunc=hashfunc)
384384

385385
@classmethod
386386
def from_public_key_recovery(cls, signature, data, curve, hashfunc=sha1,

src/ecdsa/test_keys.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ def setUpClass(cls):
120120
"-----BEGIN PUBLIC KEY-----\n"
121121
"MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEuIF30ITvF/XkVjlAgCg2D59ZtKTX\n"
122122
"Jk5i2gZR3OR6NaTFtFz1FZNCOotVe5wgmfNs\n"
123-
"-----END PUBLIC KEY-----\n")
124-
123+
"-----END PUBLIC KEY-----\n")
124+
cls.key_pem = key_str
125+
125126
cls.key_bytes = unpem(key_str)
126127
assert isinstance(cls.key_bytes, bytes)
127128
cls.vk = VerifyingKey.from_pem(key_str)
128129
cls.sk = SigningKey.from_pem(prv_key_str)
129-
130+
130131
key_str = (
131132
"-----BEGIN PUBLIC KEY-----\n"
132133
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE4H3iRbG4TSrsSRb/gusPQB/4YcN8\n"
@@ -135,6 +136,16 @@ def setUpClass(cls):
135136
)
136137
cls.vk2 = VerifyingKey.from_pem(key_str)
137138

139+
def test_custom_hashfunc(self):
140+
vk = VerifyingKey.from_der(self.key_bytes, hashlib.sha256)
141+
142+
self.assertIs(vk.default_hashfunc, hashlib.sha256)
143+
144+
def test_from_pem_with_custom_hashfunc(self):
145+
vk = VerifyingKey.from_pem(self.key_pem, hashlib.sha256)
146+
147+
self.assertIs(vk.default_hashfunc, hashlib.sha256)
148+
138149
def test_bytes(self):
139150
vk = VerifyingKey.from_der(self.key_bytes)
140151

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

168179
self.assertEqual(self.vk.to_string(), vk.to_string())
169-
170-
def test_equality_on_verifying_keys(self):
180+
181+
def test_equality_on_verifying_keys(self):
171182
self.assertEqual(self.vk, self.sk.get_verifying_key())
172-
173-
def test_inequality_on_verifying_keys(self):
183+
184+
def test_inequality_on_verifying_keys(self):
174185
self.assertNotEqual(self.vk, self.vk2)
175-
176-
def test_inequality_on_verifying_keys_not_implemented(self):
186+
187+
def test_inequality_on_verifying_keys_not_implemented(self):
177188
self.assertNotEqual(self.vk, None)
178189

179190

0 commit comments

Comments
 (0)