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

remove usage of six.b("literal") #329

Merged
merged 2 commits into from
Jan 8, 2024
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
7 changes: 3 additions & 4 deletions speed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import six
import timeit
from ecdsa.curves import curves

Expand Down Expand Up @@ -43,9 +42,9 @@ def do(setup_statements, statement):
)

for curve in [i.name for i in curves]:
S1 = "import six; from ecdsa import SigningKey, %s" % curve
S1 = "from ecdsa import SigningKey, %s" % curve
S2 = "sk = SigningKey.generate(%s)" % curve
S3 = "msg = six.b('msg')"
S3 = "msg = b'msg'"
S4 = "sig = sk.sign(msg)"
S5 = "vk = sk.get_verifying_key()"
S6 = "vk.precompute()"
Expand All @@ -61,7 +60,7 @@ def do(setup_statements, statement):
import ecdsa

c = getattr(ecdsa, curve)
sig = ecdsa.SigningKey.generate(c).sign(six.b("msg"))
sig = ecdsa.SigningKey.generate(c).sign(b"msg")
print(
prnt_form.format(
name=curve,
Expand Down
28 changes: 14 additions & 14 deletions src/ecdsa/der.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import base64
import warnings
from itertools import chain
from six import int2byte, b, text_type
from six import int2byte, text_type
from ._compat import compat26_str, str_idx_as_int


Expand All @@ -20,16 +20,16 @@ def encode_integer(r):
assert r >= 0 # can't support negative numbers yet
h = ("%x" % r).encode()
if len(h) % 2:
h = b("0") + h
h = b"0" + h
s = binascii.unhexlify(h)
num = str_idx_as_int(s, 0)
if num <= 0x7F:
return b("\x02") + encode_length(len(s)) + s
return b"\x02" + encode_length(len(s)) + s
else:
# DER integers are two's complement, so if the first byte is
# 0x80-0xff then we need an extra 0x00 byte to prevent it from
# looking negative.
return b("\x02") + encode_length(len(s) + 1) + b("\x00") + s
return b"\x02" + encode_length(len(s) + 1) + b"\x00" + s


# sentry object to check if an argument was specified (used to detect
Expand Down Expand Up @@ -91,11 +91,11 @@ def encode_bitstring(s, unused=_sentry):
raise ValueError("unused bits must be zeros in DER")
encoded_unused = int2byte(unused)
len_extra = 1
return b("\x03") + encode_length(len(s) + len_extra) + encoded_unused + s
return b"\x03" + encode_length(len(s) + len_extra) + encoded_unused + s


def encode_octet_string(s):
return b("\x04") + encode_length(len(s)) + s
return b"\x04" + encode_length(len(s)) + s


def encode_oid(first, second, *pieces):
Expand All @@ -111,7 +111,7 @@ def encode_oid(first, second, *pieces):

def encode_sequence(*encoded_pieces):
total_len = sum([len(p) for p in encoded_pieces])
return b("\x30") + encode_length(total_len) + b("").join(encoded_pieces)
return b"\x30" + encode_length(total_len) + b"".join(encoded_pieces)


def encode_number(n):
Expand All @@ -122,7 +122,7 @@ def encode_number(n):
if not b128_digits:
b128_digits.append(0)
b128_digits[-1] &= 0x7F
return b("").join([int2byte(d) for d in b128_digits])
return b"".join([int2byte(d) for d in b128_digits])


def is_sequence(string):
Expand Down Expand Up @@ -254,7 +254,7 @@ def encode_length(l):
return int2byte(l)
s = ("%x" % l).encode()
if len(s) % 2:
s = b("0") + s
s = b"0" + s
s = binascii.unhexlify(s)
llen = len(s)
return int2byte(0x80 | llen) + s
Expand Down Expand Up @@ -389,11 +389,11 @@ def unpem(pem):
if isinstance(pem, text_type): # pragma: no branch
pem = pem.encode()

d = b("").join(
d = b"".join(
[
l.strip()
for l in pem.split(b("\n"))
if l and not l.startswith(b("-----"))
for l in pem.split(b"\n")
if l and not l.startswith(b"-----")
]
)
return base64.b64decode(d)
Expand All @@ -403,7 +403,7 @@ def topem(der, name):
b64 = base64.b64encode(compat26_str(der))
lines = [("-----BEGIN %s-----\n" % name).encode()]
lines.extend(
[b64[start : start + 76] + b("\n") for start in range(0, len(b64), 76)]
[b64[start : start + 76] + b"\n" for start in range(0, len(b64), 76)]
)
lines.append(("-----END %s-----\n" % name).encode())
return b("").join(lines)
return b"".join(lines)
6 changes: 3 additions & 3 deletions src/ecdsa/ecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"""

import warnings
from six import int2byte, b
from six import int2byte
from . import ellipticcurve
from . import numbertheory
from .util import bit_length
Expand Down Expand Up @@ -280,15 +280,15 @@ def int_to_string(x): # pragma: no cover
)
assert x >= 0
if x == 0:
return b("\0")
return b"\0"
result = []
while x:
ordinal = x & 0xFF
result.append(int2byte(ordinal))
x >>= 8

result.reverse()
return b("").join(result)
return b"".join(result)


def string_to_int(s): # pragma: no cover
Expand Down
8 changes: 4 additions & 4 deletions src/ecdsa/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import binascii
from hashlib import sha1
import os
from six import PY2, b
from six import PY2
from . import ecdsa, eddsa
from . import der, ssh
from . import rfc6979
Expand Down Expand Up @@ -1044,7 +1044,7 @@ def from_der(cls, string, hashfunc=sha1, valid_curve_encodings=None):
curve = None

s, empty = der.remove_sequence(s)
if empty != b(""):
if empty != b"":
raise der.UnexpectedDER(
"trailing junk after DER privkey: %s" % binascii.hexlify(empty)
)
Expand Down Expand Up @@ -1115,7 +1115,7 @@ def from_der(cls, string, hashfunc=sha1, valid_curve_encodings=None):
# Unpack the ECPrivateKey to get to the key data octet string,
# and rejoin the ssleay parsing path.
s, empty = der.remove_sequence(s)
if empty != b(""):
if empty != b"":
raise der.UnexpectedDER(
"trailing junk after DER privkey: %s"
% binascii.hexlify(empty)
Expand Down Expand Up @@ -1155,7 +1155,7 @@ def from_der(cls, string, hashfunc=sha1, valid_curve_encodings=None):
# our from_string method likes fixed-length privkey strings
if len(privkey_str) < curve.baselen:
privkey_str = (
b("\x00") * (curve.baselen - len(privkey_str)) + privkey_str
b"\x00" * (curve.baselen - len(privkey_str)) + privkey_str
)
return cls.from_string(privkey_str, curve, hashfunc)

Expand Down
39 changes: 19 additions & 20 deletions src/ecdsa/test_der.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
except ImportError:
import unittest
import sys
from six import b
import hypothesis.strategies as st
from hypothesis import given, settings
import pytest
Expand All @@ -33,44 +32,44 @@ class TestRemoveInteger(unittest.TestCase):
# interpreted as negative, check if those errors are detected
def test_non_minimal_encoding(self):
with self.assertRaises(UnexpectedDER):
remove_integer(b("\x02\x02\x00\x01"))
remove_integer(b"\x02\x02\x00\x01")

def test_negative_with_high_bit_set(self):
with self.assertRaises(UnexpectedDER):
remove_integer(b("\x02\x01\x80"))
remove_integer(b"\x02\x01\x80")

def test_minimal_with_high_bit_set(self):
val, rem = remove_integer(b("\x02\x02\x00\x80"))
val, rem = remove_integer(b"\x02\x02\x00\x80")

self.assertEqual(val, 0x80)
self.assertEqual(rem, b"")

def test_two_zero_bytes_with_high_bit_set(self):
with self.assertRaises(UnexpectedDER):
remove_integer(b("\x02\x03\x00\x00\xff"))
remove_integer(b"\x02\x03\x00\x00\xff")

def test_zero_length_integer(self):
with self.assertRaises(UnexpectedDER):
remove_integer(b("\x02\x00"))
remove_integer(b"\x02\x00")

def test_empty_string(self):
with self.assertRaises(UnexpectedDER):
remove_integer(b(""))
remove_integer(b"")

def test_encoding_of_zero(self):
val, rem = remove_integer(b("\x02\x01\x00"))
val, rem = remove_integer(b"\x02\x01\x00")

self.assertEqual(val, 0)
self.assertEqual(rem, b"")

def test_encoding_of_127(self):
val, rem = remove_integer(b("\x02\x01\x7f"))
val, rem = remove_integer(b"\x02\x01\x7f")

self.assertEqual(val, 127)
self.assertEqual(rem, b"")

def test_encoding_of_128(self):
val, rem = remove_integer(b("\x02\x02\x00\x80"))
val, rem = remove_integer(b"\x02\x02\x00\x80")

self.assertEqual(val, 128)
self.assertEqual(rem, b"")
Expand All @@ -93,37 +92,37 @@ class TestReadLength(unittest.TestCase):
# form and lengths above that encoded with minimal number of bytes
# necessary
def test_zero_length(self):
self.assertEqual((0, 1), read_length(b("\x00")))
self.assertEqual((0, 1), read_length(b"\x00"))

def test_two_byte_zero_length(self):
with self.assertRaises(UnexpectedDER):
read_length(b("\x81\x00"))
read_length(b"\x81\x00")

def test_two_byte_small_length(self):
with self.assertRaises(UnexpectedDER):
read_length(b("\x81\x7f"))
read_length(b"\x81\x7f")

def test_long_form_with_zero_length(self):
with self.assertRaises(UnexpectedDER):
read_length(b("\x80"))
read_length(b"\x80")

def test_smallest_two_byte_length(self):
self.assertEqual((128, 2), read_length(b("\x81\x80")))
self.assertEqual((128, 2), read_length(b"\x81\x80"))

def test_zero_padded_length(self):
with self.assertRaises(UnexpectedDER):
read_length(b("\x82\x00\x80"))
read_length(b"\x82\x00\x80")

def test_two_three_byte_length(self):
self.assertEqual((256, 3), read_length(b"\x82\x01\x00"))

def test_empty_string(self):
with self.assertRaises(UnexpectedDER):
read_length(b(""))
read_length(b"")

def test_length_overflow(self):
with self.assertRaises(UnexpectedDER):
read_length(b("\x83\x01\x00"))
read_length(b"\x83\x01\x00")


class TestEncodeBitstring(unittest.TestCase):
Expand Down Expand Up @@ -270,10 +269,10 @@ def test_bytearray(self):
class TestEncodeOid(unittest.TestCase):
def test_pub_key_oid(self):
oid_ecPublicKey = encode_oid(1, 2, 840, 10045, 2, 1)
self.assertEqual(hexlify(oid_ecPublicKey), b("06072a8648ce3d0201"))
self.assertEqual(hexlify(oid_ecPublicKey), b"06072a8648ce3d0201")

def test_nist224p_oid(self):
self.assertEqual(hexlify(NIST224p.encoded_oid), b("06052b81040021"))
self.assertEqual(hexlify(NIST224p.encoded_oid), b"06052b81040021")

def test_nist256p_oid(self):
self.assertEqual(
Expand Down
1 change: 0 additions & 1 deletion src/ecdsa/test_jacobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import unittest

import os
import sys
import signal
import pytest
import threading
Expand Down
Loading
Loading