diff --git a/ALLFUNCTIONS.md b/ALLFUNCTIONS.md index 4be1b2a..c1da861 100644 --- a/ALLFUNCTIONS.md +++ b/ALLFUNCTIONS.md @@ -39,3 +39,5 @@ Generates a UUID using version 4 by default but you can choose. ## external_verbose_output() by @hamdivazim If you are printing a lot of data, you can use this method to write the output to log file. +## get_hash() by @MKM12345 +This takes a string as input, hashes it using the SHA-256 algorithm, and returns the hexadecimal representation of the hash value. diff --git a/usefulib/_usefulibs.py b/usefulib/_usefulibs.py index 9326526..c913f02 100644 --- a/usefulib/_usefulibs.py +++ b/usefulib/_usefulibs.py @@ -13,6 +13,7 @@ import string # generate_random_string() import uuid # generateUUID() import os # external_verbose_output() +import hashlib # get_hash() """""" @@ -133,4 +134,9 @@ def external_verbose_output(data, path="data.log"): with open(path, "w") as f: f.write("# Logged by usefulibs.external_verbose_output()\n\n") - f.write(data) \ No newline at end of file + f.write(data) +def get_hash(string): + """ + @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. + """ + return hashlib.sha256(string.encode('utf-8')).hexdigest() diff --git a/usefulib/tests.py b/usefulib/tests.py index cba0b12..b19e339 100644 --- a/usefulib/tests.py +++ b/usefulib/tests.py @@ -9,6 +9,7 @@ """ import unittest +import _usefulibs from _usefulibs import * class TestUsefulibs(unittest.TestCase): @@ -73,7 +74,16 @@ def test_external_verbose_output(self): with open(R"usefulib\temp_data\ext_verbose_test.log", "r") as f: self.assertEqual(f.read(), "# Logged by usefulibs.external_verbose_output()\n\nTest Data\n1 2 3\na b c") + def test_get_hash(self): + """ @MKM12345 """ + password = "myPassword123" + password_hash = get_hash(password) + user_input = input("Please enter your password: ") + if get_hash(user_input) == password_hash: + print("Welcome!") + else: + print("Incorrect password") if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main()