Skip to content

Commit a7cb6c8

Browse files
Merge pull request #1 from MKM12345/main
New get_hash() Function
2 parents 3e2b87a + 6f0ace9 commit a7cb6c8

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

ALLFUNCTIONS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ Generates a UUID using version 4 by default but you can choose.
3939

4040
## external_verbose_output() by @hamdivazim
4141
If you are printing a lot of data, you can use this method to write the output to log file.
42+
## get_hash() by @MKM12345
43+
This takes a string as input, hashes it using the SHA-256 algorithm, and returns the hexadecimal representation of the hash value.

usefulib/_usefulibs.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import string # generate_random_string()
1414
import uuid # generateUUID()
1515
import os # external_verbose_output()
16+
import hashlib # get_hash()
1617

1718
""""""
1819

@@ -133,4 +134,9 @@ def external_verbose_output(data, path="data.log"):
133134

134135
with open(path, "w") as f:
135136
f.write("# Logged by usefulibs.external_verbose_output()\n\n")
136-
f.write(data)
137+
f.write(data)
138+
def get_hash(string):
139+
"""
140+
@MKM12345 - This function takes a string as input, hashes it using the SHA-256 algorithm, and returns the hexadecimal representation of the hash value. Useful for developers that one to store strings without actually having to store them.
141+
"""
142+
return hashlib.sha256(string.encode('utf-8')).hexdigest()

usefulib/tests.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
import unittest
12+
import _usefulibs
1213
from _usefulibs import *
1314

1415
class TestUsefulibs(unittest.TestCase):
@@ -73,7 +74,16 @@ def test_external_verbose_output(self):
7374

7475
with open(R"usefulib\temp_data\ext_verbose_test.log", "r") as f:
7576
self.assertEqual(f.read(), "# Logged by usefulibs.external_verbose_output()\n\nTest Data\n1 2 3\na b c")
77+
def test_get_hash(self):
78+
""" @MKM12345 """
79+
password = "myPassword123"
80+
password_hash = get_hash(password)
81+
user_input = input("Please enter your password: ")
82+
if get_hash(user_input) == password_hash:
83+
print("Welcome!")
84+
else:
85+
print("Incorrect password")
7686

7787

7888
if __name__ == "__main__":
79-
unittest.main()
89+
unittest.main()

0 commit comments

Comments
 (0)