4646K2 = const (0x8F1BBCDC )
4747K3 = const (0xCA62C1D6 )
4848
49+
4950def _getbuf (data ):
5051 """Converts data into ascii,
5152 returns bytes of data.
5253 :param str bytes bytearray data: Data to convert.
5354
5455 """
5556 if isinstance (data , str ):
56- return data .encode (' ascii' )
57+ return data .encode (" ascii" )
5758 return bytes (data )
5859
5960
@@ -63,7 +64,8 @@ def _left_rotate(n, b):
6364 :param int b: Desired rotation amount, in bits.
6465
6566 """
66- return ((n << b ) | (n >> (32 - b ))) & 0xffffffff
67+ return ((n << b ) | (n >> (32 - b ))) & 0xFFFFFFFF
68+
6769
6870# pylint: disable=invalid-name, too-many-arguments
6971def _hash_computation (chunk , h0 , h1 , h2 , h3 , h4 ):
@@ -79,7 +81,7 @@ def _hash_computation(chunk, h0, h1, h2, h3, h4):
7981
8082 # Break chunk into sixteen 4-byte big-endian words w[i]
8183 for i in range (16 ):
82- w [i ] = struct .unpack (b'>I' , chunk [i * 4 : i * 4 + 4 ])[0 ]
84+ w [i ] = struct .unpack (b">I" , chunk [i * 4 : i * 4 + 4 ])[0 ]
8385
8486 # Extend the sixteen 4-byte words into eighty 4-byte words
8587 for i in range (16 , 80 ):
@@ -107,42 +109,45 @@ def _hash_computation(chunk, h0, h1, h2, h3, h4):
107109 f = b ^ c ^ d
108110 k = K3
109111
110- a , b , c , d , e = ((_left_rotate (a , 5 ) + f + e + k + w [i ]) & 0xffffffff ,
111- a , _left_rotate (b , 30 ), c , d )
112+ a , b , c , d , e = (
113+ (_left_rotate (a , 5 ) + f + e + k + w [i ]) & 0xFFFFFFFF ,
114+ a ,
115+ _left_rotate (b , 30 ),
116+ c ,
117+ d ,
118+ )
112119
113120 # Add to chunk's hash result so far
114- h0 = (h0 + a ) & 0xffffffff
115- h1 = (h1 + b ) & 0xffffffff
116- h2 = (h2 + c ) & 0xffffffff
117- h3 = (h3 + d ) & 0xffffffff
118- h4 = (h4 + e ) & 0xffffffff
121+ h0 = (h0 + a ) & 0xFFFFFFFF
122+ h1 = (h1 + b ) & 0xFFFFFFFF
123+ h2 = (h2 + c ) & 0xFFFFFFFF
124+ h3 = (h3 + d ) & 0xFFFFFFFF
125+ h4 = (h4 + e ) & 0xFFFFFFFF
119126
120127 return h0 , h1 , h2 , h3 , h4
121128
122129
123130# pylint: disable=too-few-public-methods, invalid-name
124- class sha1 () :
131+ class sha1 :
125132 """SHA-1 Hash Object
126133
127134 """
135+
128136 digest_size = SHA_DIGESTSIZE
129137 block_size = SHA_BLOCKSIZE
130138 name = "sha1"
139+
131140 def __init__ (self , data = None ):
132141 """Construct a SHA-1 hash object.
133142 :param bytes data: Optional data to process
134143
135144 """
136145 # Initial Digest Variables
137- self ._h = (0x67452301 ,
138- 0xEFCDAB89 ,
139- 0x98BADCFE ,
140- 0x10325476 ,
141- 0xC3D2E1F0 )
146+ self ._h = (0x67452301 , 0xEFCDAB89 , 0x98BADCFE , 0x10325476 , 0xC3D2E1F0 )
142147
143148 # bytes object with 0 <= len < 64 used to store the end of the message
144149 # if the message length is not congruent to 64
145- self ._unprocessed = b''
150+ self ._unprocessed = b""
146151
147152 # Length in bytes of all data that has been processed so far
148153 self ._msg_byte_len = 0
@@ -159,15 +164,15 @@ def _create_digest(self):
159164 message_len = self ._msg_byte_len + len (message )
160165
161166 # add trailing '1' bit (+ 0's padding) to string [FIPS 5.1.1]
162- message += b' \x80 '
167+ message += b" \x80 "
163168
164169 # append 0 <= k < 512 bits '0', so that the resulting message length (in bytes)
165170 # is congruent to 56 (mod 64)
166- message += b' \x00 ' * ((56 - (message_len + 1 ) % 64 ) % 64 )
171+ message += b" \x00 " * ((56 - (message_len + 1 ) % 64 ) % 64 )
167172
168173 # append ml, the original message length, as a 64-bit big-endian integer.
169174 message_bit_length = message_len * 8
170- message += struct .pack (b'>Q' , message_bit_length )
175+ message += struct .pack (b">Q" , message_bit_length )
171176
172177 # Process the final chunk
173178 h = _hash_computation (message [:64 ], * self ._h )
@@ -205,11 +210,11 @@ def digest(self):
205210 method so far.
206211
207212 """
208- return b'' .join (struct .pack (b'>I' , h ) for h in self ._create_digest ())
213+ return b"" .join (struct .pack (b">I" , h ) for h in self ._create_digest ())
209214
210215 def hexdigest (self ):
211216 """Like digest() except the digest is returned as a string object of
212217 double length, containing only hexadecimal digits.
213218
214219 """
215- return '' .join ([' %.2x' % i for i in self .digest ()])
220+ return "" .join ([" %.2x" % i for i in self .digest ()])
0 commit comments