Skip to content

Commit c983649

Browse files
committed
deprecate digest_integer() and related methods
1 parent c17d7e7 commit c983649

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/ecdsa/ecdsa.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
modified as part of the python-ecdsa package.
6565
"""
6666

67+
import warnings
6768
from six import int2byte, b
6869
from . import ellipticcurve
6970
from . import numbertheory
@@ -269,8 +270,14 @@ def sign(self, hash, random_k):
269270
return Signature(r, s)
270271

271272

272-
def int_to_string(x):
273+
def int_to_string(x): # pragma: no cover
273274
"""Convert integer x into a string of bytes, as per X9.62."""
275+
# deprecated in 0.19
276+
warnings.warn(
277+
"Function is unused in library code. If you use this code, "
278+
"change to util.string_to_number.",
279+
DeprecationWarning,
280+
)
274281
assert x >= 0
275282
if x == 0:
276283
return b("\0")
@@ -284,8 +291,14 @@ def int_to_string(x):
284291
return b("").join(result)
285292

286293

287-
def string_to_int(s):
294+
def string_to_int(s): # pragma: no cover
288295
"""Convert a string of bytes into an integer, as per X9.62."""
296+
# deprecated in 0.19
297+
warnings.warn(
298+
"Function is unused in library code. If you use this code, "
299+
"change to util.number_to_string.",
300+
DeprecationWarning,
301+
)
289302
result = 0
290303
for c in s:
291304
if not isinstance(c, int):
@@ -294,9 +307,16 @@ def string_to_int(s):
294307
return result
295308

296309

297-
def digest_integer(m):
310+
def digest_integer(m): # pragma: no cover
298311
"""Convert an integer into a string of bytes, compute
299312
its SHA-1 hash, and convert the result to an integer."""
313+
# deprecated in 0.19
314+
warnings.warn(
315+
"Function is unused in library code. If you use this code, "
316+
"change to a one-liner with util.number_to_string and "
317+
"util.string_to_number methods.",
318+
DeprecationWarning,
319+
)
300320
#
301321
# I don't expect this function to be used much. I wrote
302322
# it in order to be able to duplicate the examples

src/ecdsa/test_ecdsa.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,13 @@ def test_signature_validity(gen, msg, qx, qy, r, s, expected):
598598
elliptic curve of `gen`, `r` and `s` are the signature, and
599599
`expected` is True iff the signature is expected to be valid."""
600600
pubk = Public_key(gen, ellipticcurve.Point(gen.curve(), qx, qy))
601-
assert expected == pubk.verifies(digest_integer(msg), Signature(r, s))
601+
with pytest.warns(DeprecationWarning) as warns:
602+
msg_dgst = digest_integer(msg)
603+
assert len(warns) == 3
604+
assert "unused" in warns[0].message.args[0]
605+
assert "unused" in warns[1].message.args[0]
606+
assert "unused" in warns[2].message.args[0]
607+
assert expected == pubk.verifies(msg_dgst, Signature(r, s))
602608

603609

604610
@pytest.mark.parametrize(
@@ -607,7 +613,13 @@ def test_signature_validity(gen, msg, qx, qy, r, s, expected):
607613
def test_pk_recovery(gen, msg, r, s, qx, qy, expected):
608614
del expected
609615
sign = Signature(r, s)
610-
pks = sign.recover_public_keys(digest_integer(msg), gen)
616+
with pytest.warns(DeprecationWarning) as warns:
617+
msg_dgst = digest_integer(msg)
618+
assert len(warns) == 3
619+
assert "unused" in warns[0].message.args[0]
620+
assert "unused" in warns[1].message.args[0]
621+
assert "unused" in warns[2].message.args[0]
622+
pks = sign.recover_public_keys(msg_dgst, gen)
611623

612624
assert pks
613625

@@ -675,4 +687,8 @@ def test_sig_verify(args):
675687

676688

677689
def test_int_to_string_with_zero():
678-
assert int_to_string(0) == b"\x00"
690+
with pytest.warns(DeprecationWarning) as warns:
691+
assert int_to_string(0) == b"\x00"
692+
693+
assert len(warns) == 1
694+
assert "unused" in warns[0].message.args[0]

0 commit comments

Comments
 (0)