Skip to content

Commit 7e5c5ec

Browse files
committed
bytes input for SigningKey.from_string()
1 parent 8e95d84 commit 7e5c5ec

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/ecdsa/keys.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ def from_string(cls, string, curve=NIST192p, hashfunc=sha1):
737737
:return: Initialised SigningKey object
738738
:rtype: SigningKey
739739
"""
740+
string = normalise_bytes(string)
740741
if len(string) != curve.baselen:
741742
raise MalformedPointError(
742743
"Invalid length of private key, received {0}, expected {1}"

src/ecdsa/test_keys.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,32 @@ def test_VerifyingKey_verify(
223223
sig = mod_apply(signature)
224224

225225
assert vrf_mthd(sig, fun(vrf_data), sigdecode=decoder)
226+
227+
228+
# test SigningKey.from_string()
229+
prv_key_bytes = (b'^\xc8B\x0b\xd6\xef\x92R\xa9B\xe9\x89\x04<\xa2'
230+
b'\x9fV\x1f\xa5%w\x0e\xb1\xc5')
231+
assert len(prv_key_bytes) == 24
232+
keys = []
233+
for modifier, convert in [
234+
("bytes", lambda x: x),
235+
("bytes memoryview", buffer),
236+
("bytearray", bytearray),
237+
("bytearray memoryview", lambda x: buffer(bytearray(x))),
238+
("array.array of bytes", lambda x: array.array('B', x)),
239+
("array.array of bytes memoryview",
240+
lambda x: buffer(array.array('B', x))),
241+
("array.array of ints", lambda x: array.array('I', x)),
242+
("array.array of ints memoryview",
243+
lambda x: buffer(array.array('I', x)))
244+
]:
245+
keys.append(pytest.param(
246+
convert,
247+
id=modifier))
248+
249+
@pytest.mark.parametrize("convert", keys)
250+
def test_SigningKey_from_string(convert):
251+
key = convert(prv_key_bytes)
252+
sk = SigningKey.from_string(key)
253+
254+
assert sk.to_string() == prv_key_bytes

0 commit comments

Comments
 (0)