Skip to content

Commit ff37722

Browse files
committed
bytes input for VerifyingKey.from_string()
make the bytearray work on pythohn2.6 make multi-byte array.array objects work on all versions
1 parent 7a8237e commit ff37722

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

src/ecdsa/_compat.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Common functions for providing cross-python version compatibility.
3+
"""
4+
import sys
5+
6+
7+
if sys.version_info < (3, 0):
8+
def normalise_bytes(buffer_object):
9+
"""Cast the input into array of bytes."""
10+
return buffer(buffer_object)
11+
12+
13+
else:
14+
def normalise_bytes(buffer_object):
15+
"""Cast the input into array of bytes."""
16+
return memoryview(buffer_object).cast('B')

src/ecdsa/keys.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@
5555
Abstract Syntax Notation 1 is a standard description language for
5656
specifying serialisation and deserialisation of data structures in a
5757
portable and cross-platform way.
58+
59+
bytes-like object
60+
All the types that implement the buffer protocol. That includes
61+
``str`` (only on python2), ``bytes``, ``bytesarray``, ``array.array`
62+
and ``memoryview`` of those objects.
63+
Please note that ``array.array` serialisation (converting it to byte
64+
string) is endianess dependant! Signature computed over ``array.array``
65+
of integers on a big-endian system will not be verified on a
66+
little-endian system and vice-versa.
5867
"""
5968

6069
import binascii
@@ -70,6 +79,7 @@
7079
from .util import string_to_number, number_to_string, randrange
7180
from .util import sigencode_string, sigdecode_string
7281
from .util import oid_ecPublicKey, encoded_oid_ecPublicKey, MalformedSignature
82+
from ._compat import normalise_bytes
7383

7484

7585
__all__ = ["BadSignatureError", "BadDigestError", "VerifyingKey", "SigningKey",
@@ -231,8 +241,8 @@ def from_string(cls, string, curve=NIST192p, hashfunc=sha1,
231241
Python 2 days when there were no binary strings. In Python 3 the
232242
input needs to be a bytes-like object.
233243
234-
:param string: :term:`raw encoding` of the public key
235-
:type string: bytes-like object
244+
:param string: single point encoding of the public key
245+
:type string: :term:`bytes-like object`
236246
:param curve: the curve on which the public key is expected to lie
237247
:type curve: ecdsa.curves.Curve
238248
:param hashfunc: The default hash function that will be used for
@@ -245,6 +255,7 @@ def from_string(cls, string, curve=NIST192p, hashfunc=sha1,
245255
:return: Initialised VerifyingKey object
246256
:rtype: VerifyingKey
247257
"""
258+
string = normalise_bytes(string)
248259
sig_len = len(string)
249260
if sig_len == curve.verifying_key_length:
250261
point = cls._from_raw_encoding(string, curve, validate_point)

src/ecdsa/test_keys.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ def test_bytes_memoryview(self):
4444

4545
self.assertEqual(self.vk.to_string(), vk.to_string())
4646

47-
@unittest.skipIf(sys.version_info >= (2, 7), "fails on python2.6 only")
48-
@unittest.expectedFailure
49-
def test_bytearray26(self):
50-
vk = VerifyingKey.from_string(bytearray(self.key_bytes))
51-
52-
self.assertEqual(self.vk.to_string(), vk.to_string())
53-
54-
@unittest.skipUnless(sys.version_info >= (2, 7), "fails on python2.6 only")
5547
def test_bytearray(self):
5648
vk = VerifyingKey.from_string(bytearray(self.key_bytes))
5749

@@ -74,22 +66,12 @@ def test_array_array_of_bytes_memoryview(self):
7466

7567
self.assertEqual(self.vk.to_string(), vk.to_string())
7668

77-
@unittest.expectedFailure
7869
def test_array_array_of_ints(self):
7970
arr = array.array('I', self.key_bytes)
8071
vk = VerifyingKey.from_string(arr)
8172

8273
self.assertEqual(self.vk.to_string(), vk.to_string())
8374

84-
@unittest.skipIf(sys.version_info >= (3, 0), "Fails on python3")
85-
def test_array_array_of_ints_memoryview2(self):
86-
arr = array.array('I', self.key_bytes)
87-
vk = VerifyingKey.from_string(buffer(arr))
88-
89-
self.assertEqual(self.vk.to_string(), vk.to_string())
90-
91-
@unittest.expectedFailure
92-
@unittest.skipUnless(sys.version_info >= (3, 0), "Fails on python3")
9375
def test_array_array_of_ints_memoryview(self):
9476
arr = array.array('I', self.key_bytes)
9577
vk = VerifyingKey.from_string(buffer(arr))

0 commit comments

Comments
 (0)