Skip to content

Commit e5ae843

Browse files
committed
update Examples
use calling convention for open that doesn't leak file descriptors emphasise that SHA-1 is the default signature algorithm add examples with compressed and uncompressed formatting
1 parent 43e78c0 commit e5ae843

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

README.md

+44-13
Original file line numberDiff line numberDiff line change
@@ -369,43 +369,50 @@ Create a NIST192p keypair and immediately save both to disk:
369369
```python
370370
from ecdsa import SigningKey
371371
sk = SigningKey.generate()
372-
vk = sk.get_verifying_key()
373-
open("private.pem","w").write(sk.to_pem())
374-
open("public.pem","w").write(vk.to_pem())
372+
vk = sk.verifying_key
373+
with open("private.pem", "wb") as f:
374+
f.write(sk.to_pem())
375+
with open("public.pem", "wb") as f:
376+
f.write(vk.to_pem())
375377
```
376378

377-
Load a signing key from disk, use it to sign a message, and write the
378-
signature to disk:
379+
Load a signing key from disk, use it to sign a message (using SHA-1), and write
380+
the signature to disk:
379381

380382
```python
381383
from ecdsa import SigningKey
382-
sk = SigningKey.from_pem(open("private.pem").read())
383-
message = open("message","rb").read()
384+
with open("private.pem") as f:
385+
sk = SigningKey.from_pem(f.read())
386+
with open("message", "rb") as f:
387+
message = f.read()
384388
sig = sk.sign(message)
385-
open("signature","wb").write(sig)
389+
with open("signature", "wb") as f:
390+
f.write(sig)
386391
```
387392

388393
Load the verifying key, message, and signature from disk, and verify the
389-
signature:
394+
signature (assume SHA-1 hash):
390395

391396
```python
392397
from ecdsa import VerifyingKey, BadSignatureError
393398
vk = VerifyingKey.from_pem(open("public.pem").read())
394-
message = open("message","rb").read()
395-
sig = open("signature","rb").read()
399+
with open("message", "rb") as f:
400+
message = f.read()
401+
with open("signature", "rb") as f:
402+
sig = f.read()
396403
try:
397404
vk.verify(sig, message)
398405
print "good signature"
399406
except BadSignatureError:
400407
print "BAD SIGNATURE"
401408
```
402409

403-
Create a NIST521p keypair
410+
Create a NIST521p keypair:
404411

405412
```python
406413
from ecdsa import SigningKey, NIST521p
407414
sk = SigningKey.generate(curve=NIST521p)
408-
vk = sk.get_verifying_key()
415+
vk = sk.verifying_key
409416
```
410417

411418
Create three independent signing keys from a master seed:
@@ -422,3 +429,27 @@ sk1 = make_key_from_seed("1:%s" % seed)
422429
sk2 = make_key_from_seed("2:%s" % seed)
423430
sk3 = make_key_from_seed("3:%s" % seed)
424431
```
432+
433+
Load a verifying key from disk and print it using hex encoding in
434+
uncompressed and compressed format (defined in X9.62 and SEC1 standards):
435+
436+
```python
437+
from ecdsa import VerifyingKey
438+
439+
with open("public.pem") as f:
440+
vk = VerifyingKey.from_pem(f.read())
441+
442+
print("uncompressed: {0}".format(vk.to_string("uncompressed").hex()))
443+
print("compressed: {0}".format(vk.to_string("compressed").hex()))
444+
```
445+
446+
Load a verifying key from a hex string from compressed format, output
447+
uncompressed:
448+
449+
```python
450+
from ecdsa import VerifyingKey, NIST256p
451+
452+
comp_str = '022799c0d0ee09772fdd337d4f28dc155581951d07082fb19a38aa396b67e77759'
453+
vk = VerifyingKey.from_string(bytearray.fromhex(comp_str), curve=NIST256p)
454+
print(vk.to_string("uncompressed").hex())
455+
```

0 commit comments

Comments
 (0)