@@ -27,11 +27,11 @@ over prime fields.
2727
2828This library uses only Python and the 'six' package. It is compatible with
2929Python 2.6, 2.7 and 3.3+. It also supports execution on the alternative
30- implementaions like pypy and pypy3
30+ implementations like pypy and pypy3
3131
32- To run the OpenSSL compatibility tests, the 'openssl' tool must be on your
33- $ PATH. This release has been tested successfully against both OpenSSL 0.9.8o
34- and 1.0.0a .
32+ To run the OpenSSL compatibility tests, the 'openssl' tool must be in your
33+ ` PATH ` . This release has been tested successfully against OpenSSL 0.9.8o,
34+ 1.0.0a, 1.0.2f and 1.1.1d (among others) .
3535
3636## Speed
3737
@@ -110,56 +110,61 @@ a system where os.urandom() is weak.
110110
111111## Usage
112112
113- You start by creating a SigningKey. You can use this to sign data, by passing
114- in a data string and getting back the signature (also a string). You can also
115- ask a SigningKey to give you the corresponding VerifyingKey. The VerifyingKey
116- can be used to verify a signature, by passing it both the data string and the
117- signature string: it either returns True or raises BadSignatureError.
113+ You start by creating a ` SigningKey ` . You can use this to sign data, by passing
114+ in data as a byte string and getting back the signature (also a byte string).
115+ You can also ask a ` SigningKey ` to give you the corresponding ` VerifyingKey ` .
116+ The ` VerifyingKey ` can be used to verify a signature, by passing it both the
117+ data string and the signature byte string: it either returns True or raises
118+ ` BadSignatureError ` .
118119
119120``` python
120121from ecdsa import SigningKey
121122sk = SigningKey.generate() # uses NIST192p
122- vk = sk.get_verifying_key()
123+ vk = sk.verifying_key
123124signature = sk.sign(b " message" )
124125assert vk.verify(signature, b " message" )
125126```
126127
127- Each SigningKey/ VerifyingKey is associated with a specific curve, like
128+ Each ` SigningKey ` / ` VerifyingKey ` is associated with a specific curve, like
128129NIST192p (the default one). Longer curves are more secure, but take longer to
129130use, and result in longer keys and signatures.
130131
131132``` python
132133from ecdsa import SigningKey, NIST384p
133134sk = SigningKey.generate(curve = NIST384p)
134- vk = sk.get_verifying_key()
135- signature = sk.sign(" message" )
136- assert vk.verify(signature, " message" )
135+ vk = sk.verifying_key
136+ signature = sk.sign(b " message" )
137+ assert vk.verify(signature, b " message" )
137138```
138139
139- The SigningKey can be serialized into several different formats: the shortest
140+ The ` SigningKey ` can be serialized into several different formats: the shortest
140141is to call ` s=sk.to_string() ` , and then re-create it with
141142` SigningKey.from_string(s, curve) ` . This short form does not record the
142- curve, so you must be sure to tell from_string() the same curve you used for
143- the original key. The short form of a NIST192p-based signing key is just 24
143+ curve, so you must be sure to pass to ` from_string() ` the same curve you used
144+ for the original key. The short form of a NIST192p-based signing key is just 24
144145bytes long. If a point encoding is invalid or it does not lie on the specified
145- curve, ` from_string() ` will raise MalformedPointError.
146+ curve, ` from_string() ` will raise ` MalformedPointError ` .
146147
147148``` python
148149from ecdsa import SigningKey, NIST384p
149150sk = SigningKey.generate(curve = NIST384p)
150151sk_string = sk.to_string()
151152sk2 = SigningKey.from_string(sk_string, curve = NIST384p)
152- # sk and sk2 are the same key
153+ print (sk_string.hex())
154+ print (sk2.to_string().hex())
153155```
154156
157+ Note: while the methods are called ` to_string() ` the type they return is
158+ actually ` bytes ` , the "string" part is leftover from Python 2.
159+
155160` sk.to_pem() ` and ` sk.to_der() ` will serialize the signing key into the same
156161formats that OpenSSL uses. The PEM file looks like the familiar ASCII-armored
157162` "-----BEGIN EC PRIVATE KEY-----" ` base64-encoded format, and the DER format
158163is a shorter binary form of the same data.
159164` SigningKey.from_pem()/.from_der() ` will undo this serialization. These
160165formats include the curve name, so you do not need to pass in a curve
161166identifier to the deserializer. In case the file is malformed ` from_der() `
162- and ` from_pem() ` will raise UnexpectedDER or MalformedPointError.
167+ and ` from_pem() ` will raise ` UnexpectedDER ` or` MalformedPointError ` .
163168
164169``` python
165170from ecdsa import SigningKey, NIST384p
@@ -169,52 +174,53 @@ sk2 = SigningKey.from_pem(sk_pem)
169174# sk and sk2 are the same key
170175```
171176
172- Likewise, the VerifyingKey can be serialized in the same way:
177+ Likewise, the ` VerifyingKey ` can be serialized in the same way:
173178` vk.to_string()/VerifyingKey.from_string() ` , ` to_pem()/from_pem() ` , and
174- ` to_der()/from_der() ` . The same curve= argument is needed for
179+ ` to_der()/from_der() ` . The same ` curve= ` argument is needed for
175180` VerifyingKey.from_string() ` .
176181
177182``` python
178183from ecdsa import SigningKey, VerifyingKey, NIST384p
179184sk = SigningKey.generate(curve = NIST384p)
180- vk = sk.get_verifying_key()
185+ vk = sk.verifying_key
181186vk_string = vk.to_string()
182187vk2 = VerifyingKey.from_string(vk_string, curve = NIST384p)
183188# vk and vk2 are the same key
184189
185190from ecdsa import SigningKey, VerifyingKey, NIST384p
186191sk = SigningKey.generate(curve = NIST384p)
187- vk = sk.get_verifying_key()
192+ vk = sk.verifying_key
188193vk_pem = vk.to_pem()
189194vk2 = VerifyingKey.from_pem(vk_pem)
190195# vk and vk2 are the same key
191196```
192197
193198There are a couple of different ways to compute a signature. Fundamentally,
194199ECDSA takes a number that represents the data being signed, and returns a
195- pair of numbers that represent the signature. The hashfunc= argument to
200+ pair of numbers that represent the signature. The ` hashfunc= ` argument to
196201` sk.sign() ` and ` vk.verify() ` is used to turn an arbitrary string into
197202fixed-length digest, which is then turned into a number that ECDSA can sign,
198203and both sign and verify must use the same approach. The default value is
199- hashlib.sha1, but if you use NIST256p or a longer curve, you can use
200- hashlib.sha256 instead.
204+ ` hashlib.sha1 ` , but if you use NIST256p or a longer curve, you can use
205+ ` hashlib.sha256 ` instead.
201206
202207There are also multiple ways to represent a signature. The default
203208` sk.sign() ` and ` vk.verify() ` methods present it as a short string, for
204209simplicity and minimal overhead. To use a different scheme, use the
205210` sk.sign(sigencode=) ` and ` vk.verify(sigdecode=) ` arguments. There are helper
206- funcions in the " ecdsa.util" module that can be useful here.
211+ functions in the ` ecdsa.util ` module that can be useful here.
207212
208- It is also possible to create a SigningKey from a "seed", which is
213+ It is also possible to create a ` SigningKey ` from a "seed", which is
209214deterministic. This can be used in protocols where you want to derive
210215consistent signing keys from some other secret, for example when you want
211216three separate keys and only want to store a single master secret. You should
212- start with a uniformly-distributed unguessable seed with about curve.baselen
213- bytes of entropy, and then use one of the helper functions in ecdsa.util to
217+ start with a uniformly-distributed unguessable seed with about ` curve.baselen `
218+ bytes of entropy, and then use one of the helper functions in ` ecdsa.util ` to
214219convert it into an integer in the correct range, and then finally pass it
215220into ` SigningKey.from_secret_exponent() ` , like this:
216221
217222``` python
223+ import os
218224from ecdsa import NIST384p, SigningKey
219225from ecdsa.util import randrange_from_seed__trytryagain
220226
@@ -226,7 +232,9 @@ seed = os.urandom(NIST384p.baselen) # or other starting point
226232sk1a = make_key(seed)
227233sk1b = make_key(seed)
228234# note: sk1a and sk1b are the same key
229- sk2 = make_key(" 2-" + seed) # different key
235+ assert sk1a.to_string() == sk1b.to_string()
236+ sk2 = make_key(b " 2-" + seed) # different key
237+ assert sk1a.to_string() != sk2.to_string()
230238```
231239
232240## OpenSSL Compatibility
0 commit comments