@@ -369,43 +369,50 @@ Create a NIST192p keypair and immediately save both to disk:
369369``` python
370370from ecdsa import SigningKey
371371sk = 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
381383from 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()
384388sig = sk.sign(message)
385- open (" signature" ," wb" ).write(sig)
389+ with open (" signature" , " wb" ) as f:
390+ f.write(sig)
386391```
387392
388393Load the verifying key, message, and signature from disk, and verify the
389- signature:
394+ signature (assume SHA-1 hash) :
390395
391396``` python
392397from ecdsa import VerifyingKey, BadSignatureError
393398vk = 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()
396403try :
397404 vk.verify(sig, message)
398405 print " good signature"
399406except BadSignatureError:
400407 print " BAD SIGNATURE"
401408```
402409
403- Create a NIST521p keypair
410+ Create a NIST521p keypair:
404411
405412``` python
406413from ecdsa import SigningKey, NIST521p
407414sk = SigningKey.generate(curve = NIST521p)
408- vk = sk.get_verifying_key()
415+ vk = sk.verifying_key
409416```
410417
411418Create three independent signing keys from a master seed:
@@ -422,3 +429,27 @@ sk1 = make_key_from_seed("1:%s" % seed)
422429sk2 = make_key_from_seed(" 2:%s " % seed)
423430sk3 = 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