Skip to content

Commit fe2c31b

Browse files
committed
implemented sha256_encrypt
*created a crypto class *implemented sha256 encryption (ref: https://github.com/TheAlgorithms/Python/blob/master/hashes/sha256.py)
1 parent f7a6296 commit fe2c31b

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

AUTHORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ Pratik Goyal <[email protected]>
1111
Jay Thorat <[email protected]>
1212
Rajveer Singh Bharadwaj <[email protected]>
1313
Kishan Ved <[email protected]>
14-
Arvinder Singh Dhoul <[email protected]>
14+
Arvinder Singh Dhoul <[email protected]>
15+
Parasa V Prajwal <[email protected]>

pydatastructs/strings/algorithms.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
DynamicOneDimensionalArray, OneDimensionalArray)
33
from pydatastructs.utils.misc_util import (
44
Backend, raise_if_backend_is_not_python)
5+
import struct
56

67
__all__ = [
78
'find'
9+
'Crypto'
810
]
911

1012
PRIME_NUMBER, MOD = 257, 1000000007
@@ -245,3 +247,58 @@ def _z_function(text, query):
245247
positions.append(pos)
246248

247249
return positions
250+
251+
class Crypto:
252+
@staticmethod
253+
def _right_rotate(value, shift, size=32):
254+
return (value >> shift) | (value << (size - shift)) & (2**size - 1)
255+
256+
@staticmethod
257+
def sha256_encrypt(text):
258+
# SHA-256 Constants
259+
k = [
260+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
261+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
262+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
263+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
264+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
265+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
266+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
267+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
268+
]
269+
270+
h = [
271+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
272+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
273+
]
274+
275+
message = bytearray(text, 'utf-8')
276+
length = len(message) * 8
277+
message.append(0x80)
278+
while (len(message) * 8) % 512 != 448:
279+
message.append(0)
280+
message += struct.pack('>Q', length)
281+
282+
for i in range(0, len(message), 64):
283+
chunk = message[i:i+64]
284+
w = list(struct.unpack('>16L', chunk)) + [0] * 48
285+
for j in range(16, 64):
286+
s0 = (Crypto._right_rotate(w[j-15], 7) ^ Crypto._right_rotate(w[j-15], 18) ^ (w[j-15] >> 3))
287+
s1 = (Crypto._right_rotate(w[j-2], 17) ^ Crypto._right_rotate(w[j-2], 19) ^ (w[j-2] >> 10))
288+
w[j] = (w[j-16] + s0 + w[j-7] + s1) & 0xFFFFFFFF
289+
290+
a, b, c, d, e, f, g, h0 = h
291+
292+
for j in range(64):
293+
S1 = Crypto._right_rotate(e, 6) ^ Crypto._right_rotate(e, 11) ^ Crypto._right_rotate(e, 25)
294+
ch = (e & f) ^ (~e & g)
295+
temp1 = (h0 + S1 + ch + k[j] + w[j]) & 0xFFFFFFFF
296+
S0 = Crypto._right_rotate(a, 2) ^ Crypto._right_rotate(a, 13) ^ Crypto._right_rotate(a, 22)
297+
maj = (a & b) ^ (a & c) ^ (b & c)
298+
temp2 = (S0 + maj) & 0xFFFFFFFF
299+
300+
h0, g, f, e, d, c, b, a = (g, f, e, (d + temp1) & 0xFFFFFFFF, c, b, a, (temp1 + temp2) & 0xFFFFFFFF)
301+
302+
h = [(x + y) & 0xFFFFFFFF for x, y in zip(h, [a, b, c, d, e, f, g, h0])]
303+
304+
return ''.join(f'{value:08x}' for value in h)

0 commit comments

Comments
 (0)