Skip to content

Commit 373b937

Browse files
committed
added tests for sha256
* added tests and docstring for sha256
1 parent fe2c31b commit 373b937

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Algorithms
22
==========
33

4-
.. autofunction:: pydatastructs.find
4+
.. autofunction:: pydatastructs.find
5+
6+
.. autoclass:: pydatastructs.Crypto

pydatastructs/strings/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
__all__.extend(trie.__all__)
1313

1414
from .algorithms import (
15-
find
15+
find,
16+
Crypto
1617
)
1718

1819
__all__.extend(algorithms.__all__)

pydatastructs/strings/algorithms.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import struct
66

77
__all__ = [
8-
'find'
8+
'find',
99
'Crypto'
1010
]
1111

@@ -249,12 +249,44 @@ def _z_function(text, query):
249249
return positions
250250

251251
class Crypto:
252+
252253
@staticmethod
253254
def _right_rotate(value, shift, size=32):
254255
return (value >> shift) | (value << (size - shift)) & (2**size - 1)
255256

256257
@staticmethod
257-
def sha256_encrypt(text):
258+
def sha256_encrypt(text) -> str:
259+
"""
260+
Finds the SHA256 ciphertext of the given plaintext
261+
262+
Parameters
263+
==========
264+
265+
text: str
266+
The string on which SHA256 encryption is to be performed.
267+
268+
Returns
269+
=======
270+
271+
text: str
272+
The SHA256 encoded ciphertext
273+
274+
Examples
275+
========
276+
277+
>>> from pydatastructs.strings.algorithms import Crypto
278+
>>> text = "PyDataStructs"
279+
>>> ciphertext = Crypto.sha256_encrypt(text)
280+
>>> print(ciphertext)
281+
"777a305fe4f1cfc7ce270891ec50651331e2ab6d09312b906740a5ea413bd057"
282+
283+
References
284+
==========
285+
286+
.. [1] https://en.wikipedia.org/wiki/SHA-2
287+
.. [2] https://github.com/TheAlgorithms/Python/blob/master/hashes/sha256.py
288+
289+
"""
258290
# SHA-256 Constants
259291
k = [
260292
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,

pydatastructs/strings/tests/test_algorithms.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pydatastructs.strings import find
1+
from pydatastructs.strings import find, Crypto
22

33
import random, string
44

@@ -74,3 +74,26 @@ def gen_random_string(length):
7474
if text != query:
7575
positions = find(text, query, algorithm)
7676
assert positions.size == 0
77+
78+
def _test_sha256_encrypt():
79+
80+
test_cases = [
81+
"HelloWorld",
82+
"1234567890",
83+
"abcdefABCDEF",
84+
"",
85+
"The quick brown fox jumps over the lazy dog",
86+
"Pydatastructs"
87+
]
88+
89+
expected_hashes = [
90+
"872e4e50ce9990d8b041330c47c9ddd11bec6b503ae9386a99da8584e9bb12c4",
91+
"c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646",
92+
"4a098074bfa74c04454ebbc7d286d085c420934c27ab11e9c00cc53247d9959e",
93+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
94+
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592",
95+
"1b5b22944c188c3e90d126ebd27e10d7497fbf5924f23c05775fa2dd9e1d8c86"
96+
]
97+
98+
for text, expected in zip(test_cases, expected_hashes):
99+
assert Crypto.sha256_encrypt(text) == expected

0 commit comments

Comments
 (0)