1
1
#! /usr/bin/env python
2
2
3
3
"""
4
- Implementation of Elliptic-Curve Digital Signatures.
4
+ Low level implementation of Elliptic-Curve Digital Signatures.
5
+
6
+ .. note ::
7
+ You're most likely looking for the :py:class:`~ecdsa.keys` module.
8
+ This is a low-level implementation of the ECDSA that operates on
9
+ integers, not byte strings.
5
10
6
11
Classes and methods for elliptic-curve signatures:
7
12
private keys, public keys, signatures,
8
- NIST prime-modulus curves with modulus lengths of
9
- 192, 224, 256, 384, and 521 bits.
13
+ and definitions of prime-modulus curves.
10
14
11
15
Example:
12
16
13
- # (In real-life applications, you would probably want to
14
- # protect against defects in SystemRandom.)
15
- from random import SystemRandom
16
- randrange = SystemRandom().randrange
17
+ .. code-block:: python
17
18
18
- # Generate a public/private key pair using the NIST Curve P-192:
19
+ # (In real-life applications, you would probably want to
20
+ # protect against defects in SystemRandom.)
21
+ from random import SystemRandom
22
+ randrange = SystemRandom().randrange
19
23
20
- g = generator_192
21
- n = g.order()
22
- secret = randrange( 1, n )
23
- pubkey = Public_key( g, g * secret )
24
- privkey = Private_key( pubkey, secret )
24
+ # Generate a public/private key pair using the NIST Curve P-192:
25
25
26
- # Signing a hash value:
26
+ g = generator_192
27
+ n = g.order()
28
+ secret = randrange( 1, n )
29
+ pubkey = Public_key( g, g * secret )
30
+ privkey = Private_key( pubkey, secret )
27
31
28
- hash = randrange( 1, n )
29
- signature = privkey.sign( hash, randrange( 1, n ) )
32
+ # Signing a hash value:
30
33
31
- # Verifying a signature for a hash value:
34
+ hash = randrange( 1, n )
35
+ signature = privkey.sign( hash, randrange( 1, n ) )
32
36
33
- if pubkey.verifies( hash, signature ):
34
- print_("Demo verification succeeded.")
35
- else:
36
- print_("*** Demo verification failed.")
37
+ # Verifying a signature for a hash value:
37
38
38
- # Verification fails if the hash value is modified:
39
+ if pubkey.verifies( hash, signature ):
40
+ print_("Demo verification succeeded.")
41
+ else:
42
+ print_("*** Demo verification failed.")
39
43
40
- if pubkey.verifies( hash-1, signature ):
41
- print_("**** Demo verification failed to reject tampered hash.")
42
- else:
43
- print_("Demo verification correctly rejected tampered hash.")
44
+ # Verification fails if the hash value is modified:
44
45
45
- Version of 2009.05.16.
46
+ if pubkey.verifies( hash-1, signature ):
47
+ print_("**** Demo verification failed to reject tampered hash.")
48
+ else:
49
+ print_("Demo verification correctly rejected tampered hash.")
46
50
47
51
Revision history:
48
52
2005.12.31 - Initial version.
53
+
49
54
2008.11.25 - Substantial revisions introducing new classes.
55
+
50
56
2009.05.16 - Warn against using random.randrange in real applications.
57
+
51
58
2009.05.17 - Use random.SystemRandom by default.
52
59
53
- Written in 2005 by Peter Pearson and placed in the public domain.
60
+ Originally written in 2005 by Peter Pearson and placed in the public domain,
61
+ modified as part of the python-ecdsa package.
54
62
"""
55
63
56
64
from six import int2byte , b
@@ -69,16 +77,26 @@ class InvalidPointError(RuntimeError):
69
77
70
78
71
79
class Signature (object ):
72
- """ECDSA signature."""
80
+ """
81
+ ECDSA signature.
82
+
83
+ :ivar int r: the ``r`` element of the ECDSA signature
84
+ :ivar int s: the ``s`` element of the ECDSA signature
85
+ """
73
86
74
87
def __init__ (self , r , s ):
75
88
self .r = r
76
89
self .s = s
77
90
78
91
def recover_public_keys (self , hash , generator ):
79
- """Returns two public keys for which the signature is valid
80
- hash is signed hash
81
- generator is the used generator of the signature
92
+ """
93
+ Returns two public keys for which the signature is valid
94
+
95
+ :param int hash: signed hash
96
+ :param AbstractPoint generator: is the generator used in creation
97
+ of the signature
98
+ :rtype: tuple(Public_key, Public_key)
99
+ :return: a pair of public keys that can validate the signature
82
100
"""
83
101
curve = generator .curve ()
84
102
n = generator .order ()
0 commit comments