Skip to content

Commit

Permalink
PEP8 compliance, whitespace only
Browse files Browse the repository at this point in the history
  • Loading branch information
mdxs committed Nov 18, 2015
1 parent 109b091 commit 7059e40
Show file tree
Hide file tree
Showing 11 changed files with 712 additions and 558 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -37,7 +37,8 @@ The following table shows how long this library takes to generate keypairs
(keygen=), to sign data (sign=), and to verify those signatures (verify=), on
my 2008 Mac laptop. All times are in seconds. It also shows the length of a
signature (in bytes): the verifying ("public") key is typically the same
length as the signature, and the signing ("private") key is half that length. Use "python setup.py speed" to generate this table on your own computer.
length as the signature, and the signing ("private") key is half that length.
Use "python setup.py speed" to generate this table on your own computer.

* NIST192p: siglen= 48, keygen=0.160s, sign=0.058s, verify=0.116s
* NIST224p: siglen= 56, keygen=0.230s, sign=0.086s, verify=0.165s
Expand Down
14 changes: 7 additions & 7 deletions ecdsa/__init__.py
@@ -1,14 +1,14 @@
__all__ = ["curves", "der", "ecdsa", "ellipticcurve", "keys", "numbertheory",
"test_pyecdsa", "util", "six"]
from .keys import SigningKey, VerifyingKey, BadSignatureError, BadDigestError
from .curves import NIST192p, NIST224p, NIST256p, NIST384p, NIST521p, SECP256k1

_hush_pyflakes = [SigningKey, VerifyingKey, BadSignatureError, BadDigestError,
NIST192p, NIST224p, NIST256p, NIST384p, NIST521p, SECP256k1]
del _hush_pyflakes

# This code comes from http://github.com/warner/python-ecdsa

from ._version import get_versions
__version__ = get_versions()['version']
del get_versions

__all__ = ["curves", "der", "ecdsa", "ellipticcurve", "keys", "numbertheory",
"test_pyecdsa", "util", "six"]

_hush_pyflakes = [SigningKey, VerifyingKey, BadSignatureError, BadDigestError,
NIST192p, NIST224p, NIST256p, NIST384p, NIST521p, SECP256k1]
del _hush_pyflakes
26 changes: 18 additions & 8 deletions ecdsa/curves.py
Expand Up @@ -2,17 +2,20 @@

from . import der, ecdsa


class UnknownCurveError(Exception):
pass


def orderlen(order):
return (1+len("%x"%order))//2 # bytes
return (1+len("%x" % order))//2 # bytes


# the NIST curves
class Curve:
def __init__(self, name, curve, generator, oid, openssl_name=None):
self.name = name
self.openssl_name = openssl_name # maybe None
self.openssl_name = openssl_name # maybe None
self.curve = curve
self.generator = generator
self.order = generator.order()
Expand All @@ -22,21 +25,28 @@ def __init__(self, name, curve, generator, oid, openssl_name=None):
self.oid = oid
self.encoded_oid = der.encode_oid(*oid)

NIST192p = Curve("NIST192p", ecdsa.curve_192, ecdsa.generator_192,
NIST192p = Curve("NIST192p", ecdsa.curve_192,
ecdsa.generator_192,
(1, 2, 840, 10045, 3, 1, 1), "prime192v1")
NIST224p = Curve("NIST224p", ecdsa.curve_224, ecdsa.generator_224,
NIST224p = Curve("NIST224p", ecdsa.curve_224,
ecdsa.generator_224,
(1, 3, 132, 0, 33), "secp224r1")
NIST256p = Curve("NIST256p", ecdsa.curve_256, ecdsa.generator_256,
NIST256p = Curve("NIST256p", ecdsa.curve_256,
ecdsa.generator_256,
(1, 2, 840, 10045, 3, 1, 7), "prime256v1")
NIST384p = Curve("NIST384p", ecdsa.curve_384, ecdsa.generator_384,
NIST384p = Curve("NIST384p", ecdsa.curve_384,
ecdsa.generator_384,
(1, 3, 132, 0, 34), "secp384r1")
NIST521p = Curve("NIST521p", ecdsa.curve_521, ecdsa.generator_521,
NIST521p = Curve("NIST521p", ecdsa.curve_521,
ecdsa.generator_521,
(1, 3, 132, 0, 35), "secp521r1")
SECP256k1 = Curve("SECP256k1", ecdsa.curve_secp256k1, ecdsa.generator_secp256k1,
SECP256k1 = Curve("SECP256k1", ecdsa.curve_secp256k1,
ecdsa.generator_secp256k1,
(1, 3, 132, 0, 10), "secp256k1")

curves = [NIST192p, NIST224p, NIST256p, NIST384p, NIST521p, SECP256k1]


def find_curve(oid_curve):
for c in curves:
if c.oid == oid_curve:
Expand Down
35 changes: 29 additions & 6 deletions ecdsa/der.py
Expand Up @@ -4,13 +4,17 @@
import base64
from .six import int2byte, b, integer_types, text_type


class UnexpectedDER(Exception):
pass


def encode_constructed(tag, value):
return int2byte(0xa0+tag) + encode_length(len(value)) + value


def encode_integer(r):
assert r >= 0 # can't support negative numbers yet
assert r >= 0 # can't support negative numbers yet
h = ("%x" % r).encode()
if len(h) % 2:
h = b("0") + h
Expand All @@ -24,20 +28,29 @@ def encode_integer(r):
# looking negative.
return b("\x02") + int2byte(len(s)+1) + b("\x00") + s


def encode_bitstring(s):
return b("\x03") + encode_length(len(s)) + s


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


def encode_oid(first, second, *pieces):
assert first <= 2
assert second <= 39
encoded_pieces = [int2byte(40*first+second)] + [encode_number(p)
for p in pieces]
body = b('').join(encoded_pieces)
return b('\x06') + encode_length(len(body)) + body


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)


def encode_number(n):
b128_digits = []
while n:
Expand All @@ -48,6 +61,7 @@ def encode_number(n):
b128_digits[-1] &= 0x7f
return b('').join([int2byte(d) for d in b128_digits])


def remove_constructed(string):
s0 = string[0] if isinstance(string[0], integer_types) else ord(string[0])
if (s0 & 0xe0) != 0xa0:
Expand All @@ -59,6 +73,7 @@ def remove_constructed(string):
rest = string[1+llen+length:]
return tag, body, rest


def remove_sequence(string):
if not string.startswith(b("\x30")):
n = string[0] if isinstance(string[0], integer_types) else ord(string[0])
Expand All @@ -67,6 +82,7 @@ def remove_sequence(string):
endseq = 1+lengthlength+length
return string[1+lengthlength:endseq], string[endseq:]


def remove_octet_string(string):
if not string.startswith(b("\x04")):
n = string[0] if isinstance(string[0], integer_types) else ord(string[0])
Expand All @@ -76,6 +92,7 @@ def remove_octet_string(string):
rest = string[1+llen+length:]
return body, rest


def remove_object(string):
if not string.startswith(b("\x06")):
n = string[0] if isinstance(string[0], integer_types) else ord(string[0])
Expand All @@ -95,6 +112,7 @@ def remove_object(string):
numbers.insert(1, second)
return tuple(numbers), rest


def remove_integer(string):
if not string.startswith(b("\x02")):
n = string[0] if isinstance(string[0], integer_types) else ord(string[0])
Expand All @@ -103,9 +121,10 @@ def remove_integer(string):
numberbytes = string[1+llen:1+llen+length]
rest = string[1+llen+length:]
nbytes = numberbytes[0] if isinstance(numberbytes[0], integer_types) else ord(numberbytes[0])
assert nbytes < 0x80 # can't support negative numbers yet
assert nbytes < 0x80 # can't support negative numbers yet
return int(binascii.hexlify(numberbytes), 16), rest


def read_number(string):
number = 0
llen = 0
Expand All @@ -121,16 +140,18 @@ def read_number(string):
break
return number, llen


def encode_length(l):
assert l >= 0
if l < 0x80:
return int2byte(l)
s = ("%x" % l).encode()
if len(s)%2:
s = b("0")+s
if len(s) % 2:
s = b("0") + s
s = binascii.unhexlify(s)
llen = len(s)
return int2byte(0x80|llen) + s
return int2byte(0x80 | llen) + s


def read_length(string):
num = string[0] if isinstance(string[0], integer_types) else ord(string[0])
Expand All @@ -144,6 +165,7 @@ def read_length(string):
raise UnexpectedDER("ran out of length bytes")
return int(binascii.hexlify(string[1:1+llen]), 16), 1+llen


def remove_bitstring(string):
num = string[0] if isinstance(string[0], integer_types) else ord(string[0])
if not string.startswith(b("\x03")):
Expand Down Expand Up @@ -189,11 +211,12 @@ def unpem(pem):
d = b("").join([l.strip() for l in pem.split(b("\n"))
if l and not l.startswith(b("-----"))])
return base64.b64decode(d)


def topem(der, name):
b64 = base64.b64encode(der)
lines = [("-----BEGIN %s-----\n" % name).encode()]
lines.extend([b64[start:start+64]+b("\n")
for start in range(0, len(b64), 64)])
lines.append(("-----END %s-----\n" % name).encode())
return b("").join(lines)

0 comments on commit 7059e40

Please sign in to comment.