@@ -33,11 +33,10 @@ def __init__(self, p=0, q=0, g=0, x=0, y=0):
3333 self .g = g
3434 self .private_key = x
3535 self .public_key = y
36+ if self .private_key and not self .public_key :
37+ self .public_key = powMod (g , self .private_key , p )
3638 self .key_type = "dsa"
3739
38- if p and q and p < q :
39- raise ValueError ("q is greater than p" )
40-
4140 def __len__ (self ):
4241 return numBits (self .p )
4342
@@ -82,20 +81,60 @@ def hashAndSign(self, data, hAlg="sha1"):
8281 hashData = (secureHash (bytearray (data ), hAlg ))
8382 return self .sign (hashData )
8483
85- def sign (self , data ):
84+ def sign (self , data , padding = None , hashAlg = None , saltLen = None ):
85+ """
86+ :type data: bytearray
87+ :param data: The value which will be signed (generally a binary
88+ encoding of hash output.
89+
90+ :type padding: str
91+ :param padding: Ignored, present for API compatibility with RSA
92+
93+ :type hashAlg: str
94+ :param hashAlg: name of hash that was used for calculating the bytes
95+
96+ :type saltLen: int
97+ :param saltLen: Ignored, present for API compatibility with RSA
98+ """
8699 N = numBits (self .q )
87100 digest_len = len (data ) * 8
88101 digest = bytesToNumber (data )
89102 if N < digest_len :
90103 digest >>= digest_len - N
91104
92105 k = getRandomNumber (1 , (self .q - 1 ))
106+ if gmpyLoaded or GMPY2_LOADED :
107+ k = mpz (k )
108+ digest = mpz (digest )
93109 r = powMod (self .g , k , self .p ) % self .q
94110 s = invMod (k , self .q ) * (digest + self .private_key * r ) % self .q
95111
96112 return encode_sequence (encode_integer (r ), encode_integer (s ))
97113
98- def verify (self , signature , hashData ):
114+ def verify (self , signature , hashData , padding = None , hashAlg = None ,
115+ saltLen = None ):
116+ """Verify the passed-in bytes with the signature.
117+
118+ This verifies a DSA signature on the passed-in data.
119+
120+ :type signature: bytearray
121+ :param signature: The signature.
122+
123+ :type hashData: bytearray
124+ :param hashData: The value which will be verified.
125+
126+ :type padding: str
127+ :param padding: Ignored, present for API compatibility with RSA
128+
129+ :type hashAlg: str
130+ :param hashAlg: Ignored, present for API compatibility with RSA
131+
132+ :type saltLen: str
133+ :param saltLen: Ignored, present for API compatibility with RSA
134+
135+ :rtype: bool
136+ :returns: Whether the signature matches the passed-in data.
137+ """
99138 N = numBits (self .q )
100139 digest_len = len (hashData ) * 8
101140 digest = bytesToNumber (hashData )
@@ -125,8 +164,8 @@ def verify(self, signature, hashData):
125164 w = invMod (s , self .q )
126165 u1 = (digest * w ) % self .q
127166 u2 = (r * w ) % self .q
128- v = ((powMod (self .g , u1 , self .p ) * \
129- powMod (self .public_key , u2 , self .p )) % self .p ) % self .q
167+ v = ((powMod (self .g , u1 , self .p ) * \
168+ powMod (self .public_key , u2 , self .p )) % self .p ) % self .q
130169 return r == v
131170 return False
132171
0 commit comments