@@ -274,11 +274,11 @@ def sha256_encrypt(text) -> str:
274
274
Examples
275
275
========
276
276
277
- >>> from pydatastructs.strings.algorithms import Crypto
277
+ >>> from pydatastructs import Crypto
278
278
>>> text = "PyDataStructs"
279
279
>>> ciphertext = Crypto.sha256_encrypt(text)
280
280
>>> print(ciphertext)
281
- " 777a305fe4f1cfc7ce270891ec50651331e2ab6d09312b906740a5ea413bd057"
281
+ 777a305fe4f1cfc7ce270891ec50651331e2ab6d09312b906740a5ea413bd057
282
282
283
283
References
284
284
==========
@@ -298,39 +298,39 @@ def sha256_encrypt(text) -> str:
298
298
0x19a4c116 , 0x1e376c08 , 0x2748774c , 0x34b0bcb5 , 0x391c0cb3 , 0x4ed8aa4a , 0x5b9cca4f , 0x682e6ff3 ,
299
299
0x748f82ee , 0x78a5636f , 0x84c87814 , 0x8cc70208 , 0x90befffa , 0xa4506ceb , 0xbef9a3f7 , 0xc67178f2
300
300
]
301
-
301
+
302
302
h = [
303
303
0x6a09e667 , 0xbb67ae85 , 0x3c6ef372 , 0xa54ff53a ,
304
304
0x510e527f , 0x9b05688c , 0x1f83d9ab , 0x5be0cd19
305
305
]
306
-
306
+
307
307
message = bytearray (text , 'utf-8' )
308
308
length = len (message ) * 8
309
309
message .append (0x80 )
310
310
while (len (message ) * 8 ) % 512 != 448 :
311
311
message .append (0 )
312
312
message += struct .pack ('>Q' , length )
313
-
313
+
314
314
for i in range (0 , len (message ), 64 ):
315
315
chunk = message [i :i + 64 ]
316
316
w = list (struct .unpack ('>16L' , chunk )) + [0 ] * 48
317
317
for j in range (16 , 64 ):
318
318
s0 = (Crypto ._right_rotate (w [j - 15 ], 7 ) ^ Crypto ._right_rotate (w [j - 15 ], 18 ) ^ (w [j - 15 ] >> 3 ))
319
319
s1 = (Crypto ._right_rotate (w [j - 2 ], 17 ) ^ Crypto ._right_rotate (w [j - 2 ], 19 ) ^ (w [j - 2 ] >> 10 ))
320
320
w [j ] = (w [j - 16 ] + s0 + w [j - 7 ] + s1 ) & 0xFFFFFFFF
321
-
321
+
322
322
a , b , c , d , e , f , g , h0 = h
323
-
323
+
324
324
for j in range (64 ):
325
325
S1 = Crypto ._right_rotate (e , 6 ) ^ Crypto ._right_rotate (e , 11 ) ^ Crypto ._right_rotate (e , 25 )
326
326
ch = (e & f ) ^ (~ e & g )
327
327
temp1 = (h0 + S1 + ch + k [j ] + w [j ]) & 0xFFFFFFFF
328
328
S0 = Crypto ._right_rotate (a , 2 ) ^ Crypto ._right_rotate (a , 13 ) ^ Crypto ._right_rotate (a , 22 )
329
329
maj = (a & b ) ^ (a & c ) ^ (b & c )
330
330
temp2 = (S0 + maj ) & 0xFFFFFFFF
331
-
331
+
332
332
h0 , g , f , e , d , c , b , a = (g , f , e , (d + temp1 ) & 0xFFFFFFFF , c , b , a , (temp1 + temp2 ) & 0xFFFFFFFF )
333
-
333
+
334
334
h = [(x + y ) & 0xFFFFFFFF for x , y in zip (h , [a , b , c , d , e , f , g , h0 ])]
335
-
336
- return '' .join (f'{ value :08x} ' for value in h )
335
+
336
+ return '' .join (f'{ value :08x} ' for value in h )
0 commit comments