46
46
K2 = const (0x8F1BBCDC )
47
47
K3 = const (0xCA62C1D6 )
48
48
49
+
49
50
def _getbuf (data ):
50
51
"""Converts data into ascii,
51
52
returns bytes of data.
52
53
:param str bytes bytearray data: Data to convert.
53
54
54
55
"""
55
56
if isinstance (data , str ):
56
- return data .encode (' ascii' )
57
+ return data .encode (" ascii" )
57
58
return bytes (data )
58
59
59
60
@@ -63,7 +64,8 @@ def _left_rotate(n, b):
63
64
:param int b: Desired rotation amount, in bits.
64
65
65
66
"""
66
- return ((n << b ) | (n >> (32 - b ))) & 0xffffffff
67
+ return ((n << b ) | (n >> (32 - b ))) & 0xFFFFFFFF
68
+
67
69
68
70
# pylint: disable=invalid-name, too-many-arguments
69
71
def _hash_computation (chunk , h0 , h1 , h2 , h3 , h4 ):
@@ -79,7 +81,7 @@ def _hash_computation(chunk, h0, h1, h2, h3, h4):
79
81
80
82
# Break chunk into sixteen 4-byte big-endian words w[i]
81
83
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 ]
83
85
84
86
# Extend the sixteen 4-byte words into eighty 4-byte words
85
87
for i in range (16 , 80 ):
@@ -107,42 +109,45 @@ def _hash_computation(chunk, h0, h1, h2, h3, h4):
107
109
f = b ^ c ^ d
108
110
k = K3
109
111
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
+ )
112
119
113
120
# 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
119
126
120
127
return h0 , h1 , h2 , h3 , h4
121
128
122
129
123
130
# pylint: disable=too-few-public-methods, invalid-name
124
- class sha1 () :
131
+ class sha1 :
125
132
"""SHA-1 Hash Object
126
133
127
134
"""
135
+
128
136
digest_size = SHA_DIGESTSIZE
129
137
block_size = SHA_BLOCKSIZE
130
138
name = "sha1"
139
+
131
140
def __init__ (self , data = None ):
132
141
"""Construct a SHA-1 hash object.
133
142
:param bytes data: Optional data to process
134
143
135
144
"""
136
145
# Initial Digest Variables
137
- self ._h = (0x67452301 ,
138
- 0xEFCDAB89 ,
139
- 0x98BADCFE ,
140
- 0x10325476 ,
141
- 0xC3D2E1F0 )
146
+ self ._h = (0x67452301 , 0xEFCDAB89 , 0x98BADCFE , 0x10325476 , 0xC3D2E1F0 )
142
147
143
148
# bytes object with 0 <= len < 64 used to store the end of the message
144
149
# if the message length is not congruent to 64
145
- self ._unprocessed = b''
150
+ self ._unprocessed = b""
146
151
147
152
# Length in bytes of all data that has been processed so far
148
153
self ._msg_byte_len = 0
@@ -159,15 +164,15 @@ def _create_digest(self):
159
164
message_len = self ._msg_byte_len + len (message )
160
165
161
166
# add trailing '1' bit (+ 0's padding) to string [FIPS 5.1.1]
162
- message += b' \x80 '
167
+ message += b" \x80 "
163
168
164
169
# append 0 <= k < 512 bits '0', so that the resulting message length (in bytes)
165
170
# 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 )
167
172
168
173
# append ml, the original message length, as a 64-bit big-endian integer.
169
174
message_bit_length = message_len * 8
170
- message += struct .pack (b'>Q' , message_bit_length )
175
+ message += struct .pack (b">Q" , message_bit_length )
171
176
172
177
# Process the final chunk
173
178
h = _hash_computation (message [:64 ], * self ._h )
@@ -205,11 +210,11 @@ def digest(self):
205
210
method so far.
206
211
207
212
"""
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 ())
209
214
210
215
def hexdigest (self ):
211
216
"""Like digest() except the digest is returned as a string object of
212
217
double length, containing only hexadecimal digits.
213
218
214
219
"""
215
- return '' .join ([' %.2x' % i for i in self .digest ()])
220
+ return "" .join ([" %.2x" % i for i in self .digest ()])
0 commit comments