diff --git a/enrocrypt/basic.py b/enrocrypt/basic.py index e45127b..4579805 100644 --- a/enrocrypt/basic.py +++ b/enrocrypt/basic.py @@ -1,53 +1,57 @@ from enrocrypt import error import uuid, os -def seperator(data: str): - '''Takes a String and Returns a List of the Characters. All Elements of the List are str. The List is Iterable.''' - strData = str(data) # To avoid errors - listString = list(strData) - return listString -def hexdigest(data:bytes): - '''Returns A Wrong Hex Value. Must Only Be Used With Hashing Functions As The Hex Data Is Hard To Retrive''' - to_ascii = seperator(data) - in_ascii = [] - in_str_ascii = [] - for i in to_ascii: - in_ascii.append(ord(i)) - for i in in_ascii: - in_str_ascii.append(str(i)) - to_hex = int(''.join(in_str_ascii)) - in_hex = hex(to_hex) - return in_hex +class Basic(): + def __str__(self) -> str: + return "This Class Has All the Basic Functions Of EnroCrypt" + def seperator(self,data: str): + '''Takes a String and Returns a List of the Characters. All Elements of the List are str. The List is Iterable.''' + strData = str(data) # To avoid errors + listString = list(strData) + return listString -def Hex(data): - '''Gives The Correct Value With Each Group Of Letter(s) Representing A Character - When Decoded Gives Integer. The Returned Value Will Have One More Layer Of Abstruction''' - to_ascii = seperator(data) - in_ascii = [] - in_hex = [] - for i in to_ascii: - in_ascii.append(ord(i)) - for i in in_ascii: - in_hex.append(hex(i)) - return in_hex -def to_str(data): - '''Takes The List Returned By Hex Function''' - if type(data) != type([]): - error.List() - in_int = [] - in_ascii = [] - in_str = [] - for i,_ in enumerate(data): - in_int.append(data[i]) - for i,_ in enumerate(in_int): - in_ascii.append(int(in_int[i],16)) - for i,_ in enumerate(in_ascii): - in_str.append(chr(in_ascii[i])) - return(in_str) + def hexdigest(self,data:bytes): + '''Returns A Wrong Hex Value. Must Only Be Used With Hashing Functions As The Hex Data Is Hard To Retrive''' + to_ascii = self.seperator(data) + in_ascii = [] + in_str_ascii = [] + for i in to_ascii: + in_ascii.append(ord(i)) + for i in in_ascii: + in_str_ascii.append(str(i)) + to_hex = int(''.join(in_str_ascii)) + in_hex = hex(to_hex) + return in_hex -def Sign(sign:str): - '''A Hint Of Upcomming Updates''' - return uuid.uuid5(uuid.uuid4(),sign) -def Password_Creator(): - '''This Password Generator Is For The Function Auth_Encryption. NOT for Generating Account Passwords''' - return os.urandom(12) + def Hex(self,data): + '''Gives The Correct Value With Each Group Of Letter(s) Representing A Character + When Decoded Gives Integer. The Returned Value Will Have One More Layer Of Abstruction''' + to_ascii = self.seperator(data) + in_ascii = [] + in_hex = [] + for i in to_ascii: + in_ascii.append(ord(i)) + for i in in_ascii: + in_hex.append(hex(i)) + return in_hex + def to_str(self,data): + '''Takes The List Returned By Hex Function''' + if type(data) != type([]): + error.List() + in_int = [] + in_ascii = [] + in_str = [] + for i,_ in enumerate(data): + in_int.append(data[i]) + for i,_ in enumerate(in_int): + in_ascii.append(int(in_int[i],16)) + for i,_ in enumerate(in_ascii): + in_str.append(chr(in_ascii[i])) + return(in_str) + + def Sign(self,sign:str): + '''A Hint Of Upcomming Updates''' + return uuid.uuid5(uuid.uuid4(),sign) + def Password_Creator(self): + '''This Password Generator Is For The Function Auth_Encryption. NOT for Generating Account Passwords''' + return os.urandom(12) diff --git a/enrocrypt/core.py b/enrocrypt/core.py new file mode 100644 index 0000000..1f3f8c9 --- /dev/null +++ b/enrocrypt/core.py @@ -0,0 +1,36 @@ +from typing import Any +from hashing import Hashing +from encryption import Encryption +from basic import Basic + +class Core(Hashing,Encryption,Basic): + '''Given Below Is The Syntax To Set Configurations For This Class. This Must Be Used As Is(Just Change The File Path) + + config = { + 'configs':{ + 'salt_file':"The Path Of The File Where Your Salt Is Stored" + } + }''' + def __init__(self) -> None: + self.salt = '' + def __call__(self,*args: Any): + configs = (args[0]['configs']['salt_file']) + value = self.__Set_Salt(configs) + return value + def __str__(self) -> str: + return "The Base Class Of EnroCrypt" + def __Set_Salt(self,salt:list): + try: + with open(salt,'r') as f: + salts = f.read() + self.salt = bytes(salts.encode()) + return True + except FileNotFoundError: + return Warning("No Salt File Found At The Given Location Using Random Salt") + else: + return False + def get_hash_object(self): + hashing = Hashing() + hashing(bytes(self.salt.encode())) + return hashing + \ No newline at end of file diff --git a/enrocrypt/encryption.py b/enrocrypt/encryption.py index e5431b1..7c09ef1 100644 --- a/enrocrypt/encryption.py +++ b/enrocrypt/encryption.py @@ -3,129 +3,134 @@ import base64 from cryptography.fernet import Fernet from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305 -def _Keyencryption(key:bytes): - bases = base64.standard_b64encode(key) - baseu = base64.urlsafe_b64encode(bases) - return baseu +from cryptography.hazmat.primitives.ciphers.aead import AESGCM, AESCCM -def _Keydecryption(key:bytes): - bases = base64.standard_b64decode(key) - baseu = base64.urlsafe_b64decode(bases) - return baseu +class Encryption(): + def __str__(self) -> str: + return "This Class Contains All The Encryption Functions Of EnroCrypt" + def __Keyencryption(self,key:bytes): + bases = base64.standard_b64encode(key) + baseu = base64.urlsafe_b64encode(bases) + return baseu -def _dataencryption(data:bytes): - bases = base64.standard_b64encode(data) - baseu = base64.urlsafe_b64encode(bases) - return baseu + def __Keydecryption(self,key:bytes): + bases = base64.standard_b64decode(key) + baseu = base64.urlsafe_b64decode(bases) + return baseu -def _datadecryption(data:bytes): - baseu = base64.urlsafe_b64decode(data) - bases = base64.standard_b64decode(baseu) - return bases + def __dataencryption(self,data:bytes): + bases = base64.standard_b64encode(data) + baseu = base64.urlsafe_b64encode(bases) + return baseu -def _BaseEncryption(text:bytes): - key = Fernet.generate_key() - f = Fernet(key) - e = f.encrypt(text) - return [key,e] + def __datadecryption(self,data:bytes): + baseu = base64.urlsafe_b64decode(data) + bases = base64.standard_b64decode(baseu) + return bases -def _BaseDecryption(key:bytes,text:bytes): - f = Fernet(key) - e = f.decrypt(text) - return e + def __BaseEncryption(self,text:bytes): + key = Fernet.generate_key() + f = Fernet(key) + e = f.encrypt(text) + return [key,e] -def Encrypt(Data:bytes): - basee = _BaseEncryption(Data) - e_data = _dataencryption(basee[1]) - e_key = _Keyencryption(basee[0]) - final = [] - final.append('Key →');final.append(e_key);final.append('Encrypted Data →');final.append(e_data) - return final + def __BaseDecryption(self,key:bytes,text:bytes): + f = Fernet(key) + e = f.decrypt(text) + return e -def Decrypt(Key:bytes,Encrypted_Data:bytes): - d_key = _Keydecryption(Key) - d_data = _datadecryption(Encrypted_Data) - based = _BaseDecryption(d_key,d_data) - return based + def Encrypt(self,Data:bytes): + basee = self.__BaseEncryption(Data) + e_data = self.__dataencryption(basee[1]) + e_key = self.__Keyencryption(basee[0]) + final = [] + final.append('Key →');final.append(e_key);final.append('Encrypted Data →');final.append(e_data) + return final -def Decrypt_List(Data:list): - '''Takes The List Returned By Encrypt Function "AS IS" Without Modification''' - if 'Key →' and 'Encrypted Data →' not in Data: - ModifiedError() - if len(Data) != 4: - ListIndexError() - Key = Data[1] - e_Data = Data[3] - d_key = _Keydecryption(Key) - d_data = _datadecryption(e_Data) - based = _BaseDecryption(d_key,d_data) - return based + def Decrypt(self,Key:bytes,Encrypted_Data:bytes): + d_key = self.__Keydecryption(Key) + d_data = self.__datadecryption(Encrypted_Data) + based = self.__BaseDecryption(d_key,d_data) + return based -def FileEncryption(Path:str,KeyFilePath:str): - '''Make Sure The Path You Give Has "\\" insted of just "\". KeyFilePath Takes the path where you have to keep the key for the encrypted file, Path Takes the path of the file which has to be encrypted - Note: You Can Use The Key File To Store Multiple Keys But The Keys Must Not Be of The Same File Else You Will Get A Invalid Key Error''' - file1 = open(Path,'r') - data = (file1.read()).encode() - with open(Path,'w') as file: - e_file = Encrypt(bytes(data)) - key = str(e_file[1]) - e_data = str(e_file[3]) - file.write(e_data) + def Decrypt_List(self,Data:list): + '''Takes The List Returned By Encrypt Function "AS IS" Without Modification''' + if 'Key →' and 'Encrypted Data →' not in Data: + ModifiedError() + if len(Data) != 4: + ListIndexError() + Key = Data[1] + e_Data = Data[3] + d_key = self.__Keydecryption(Key) + d_data = self.__datadecryption(e_Data) + based = self.__BaseDecryption(d_key,d_data) + return based + + def FileEncryption(self,Path:str,KeyFilePath:str): + '''Make Sure The Path You Give Has "\\" insted of just "\". KeyFilePath Takes the path where you have to keep the key for the encrypted file, Path Takes the path of the file which has to be encrypted + Note: You Can Use The Key File To Store Multiple Keys But The Keys Must Not Be of The Same File Else You Will Get A Invalid Key Error''' + file1 = open(Path,'r') + data = (file1.read()).encode() + with open(Path,'w') as file: + e_file = self.Encrypt(bytes(data)) + key = str(e_file[1]) + e_data = str(e_file[3]) + file.write(e_data) + if KeyFilePath is not None: + with open(KeyFilePath,'a') as keyf: + keyf.write(Path+':'+';'+':'+';') + keyf.write(key) + keyf.write('\n') + if KeyFilePath is None: + NoKeyFile() + file1.close() + + def FileDecryption(self,Path:str,KeyFilePath:str): + '''Path: The Path Of The Encrypted File + KeyFilePath: Path Of That key File Where The Decryption Key Is Stored For The File ''' + import ast + file1 = open(Path,'r') if KeyFilePath is not None: - with open(KeyFilePath,'a') as keyf: - keyf.write(Path+':'+';'+':'+';') - keyf.write(key) - keyf.write('\n') + with open(KeyFilePath,'r') as keyf: + e_data = ast.literal_eval(file1.read()) + alls = keyf.read() + splited =alls.split('\n') + for char,i in enumerate(splited): + a = i.split(':;:;') + if a[char] == Path: + key = ast.literal_eval(a[char+1]) + break + n_data = str(self.Decrypt(key,e_data).decode()) + file2 = open(Path,'w') + file2.write(n_data) + if KeyFilePath is None: NoKeyFile() - file1.close() - -def FileDecryption(Path:str,KeyFilePath:str): - '''Path: The Path Of The Encrypted File - KeyFilePath: Path Of That key File Where The Decryption Key Is Stored For The File ''' - import ast - file1 = open(Path,'r') - if KeyFilePath is not None: - with open(KeyFilePath,'r') as keyf: - e_data = ast.literal_eval(file1.read()) - alls = keyf.read() - splited =alls.split('\n') - for char,i in enumerate(splited): - a = i.split(':;:;') - if a[char] == Path: - key = ast.literal_eval(a[char+1]) - break - n_data = str(Decrypt(key,e_data).decode()) - file2 = open(Path,'w') - file2.write(n_data) - - if KeyFilePath is None: - NoKeyFile() -def _Base_Auth_Encryption(data:bytes,optd:bytes = None): - password = Password_Creator() - key = ChaCha20Poly1305.generate_key() - encryptor = ChaCha20Poly1305(key) - ed = encryptor.encrypt(password,data,optd) - print([key,password,ed]) - return [key,password,ed] + def __Base_Auth_Encryption(self,data:bytes,optd:bytes = None): + password = Password_Creator() + key = ChaCha20Poly1305.generate_key() + encryptor = ChaCha20Poly1305(key) + ed = encryptor.encrypt(password,data,optd) + print([key,password,ed]) + return [key,password,ed] -def Auth_Encryption(data:bytes,optd:bytes = None): - '''You Can't Choose Your Passwords For Security Reasons. - data: The Data You Wnat To Encrypt. - optd: Optional Data You Wnat To Give. - We Recommend You Give The optd.''' - basee = _Base_Auth_Encryption(data,optd) - key = _Keyencryption(basee[0]) - password = _Keyencryption(basee[1]) - encd = _dataencryption(basee[2]) - final = [] - final.append('Key →');final.append(key);final.append('Password →');final.append(password);final.append('Encrypted Data →');final.append(encd);final.append('Optional Data →');final.append(optd) - return final -def Auth_Decryption(key:bytes,password:bytes,data:bytes,optd:bytes = None): - Key = _Keydecryption(key) - Password = _Keydecryption(password) - Data = _datadecryption(data) - decryptor = ChaCha20Poly1305(Key) - value = decryptor.decrypt(Password,Data,optd) - return value + def Auth_Encryption(self,data:bytes,optd:bytes = None): + '''You Can't Choose Your Passwords For Security Reasons. + data: The Data You Wnat To Encrypt. + optd: Optional Data You Wnat To Give. + We Recommend You Give The optd.''' + basee = self.__Base_Auth_Encryption(data,optd) + key = self.__Keyencryption(basee[0]) + password = self.__Keyencryption(basee[1]) + encd = self.__dataencryption(basee[2]) + final = [] + final.append('Key →');final.append(key);final.append('Password →');final.append(password);final.append('Encrypted Data →');final.append(encd);final.append('Optional Data →');final.append(optd) + return final + def Auth_Decryption(self,key:bytes,password:bytes,data:bytes,optd:bytes = None): + Key = self.__Keydecryption(key) + Password = self.__Keydecryption(password) + Data = self.__datadecryption(data) + decryptor = ChaCha20Poly1305(Key) + value = decryptor.decrypt(Password,Data,optd) + return value \ No newline at end of file diff --git a/enrocrypt/hashing.py b/enrocrypt/hashing.py index 41dc23d..2631d2c 100644 --- a/enrocrypt/hashing.py +++ b/enrocrypt/hashing.py @@ -1,60 +1,82 @@ -from enrocrypt.basic import seperator import hashlib, base64, uuid from cryptography.hazmat.primitives import hashes -from cryptography.hazmat.backends.interfaces import HashBackend -def Standard_Multi_Hash(Data:str): - '''Inreversable Salted Hash Function Don't Use If U Want To Get The Content Back''' - a = hashlib.sha256(); a.update(bytes(Data.encode())); b = [] - base = hashlib.sha512() - md = hashlib.md5() - b.append(str(a.digest()).split("'")[1]) - b[0] = str(base64.urlsafe_b64encode(bytes(b[0].encode()))).split("'")[1] - base.update(bytes(b[0].encode())) - md.update(base.digest()) - b[0]=str(base64.urlsafe_b64encode(base64.standard_b64encode(md.digest()))).split("'")[1] - salt = ['H', 'c', 'D', 'L', 'b', 'M', 'S', 'a', 'N', 'q', 'K', 'j', 'V', 'd', 'O', 'W', 'x'] - c = (b[0].split("G"))or(b[0].split("g"))or(b[0].split("v"))or(b[0].split("x")); d=[]; e=[] - for i in range(len(c)): a = salt[i]; b = c[i]; c[i] = b+a - for i in range(len(c)): - try: d.append(c[i+1]) - except: d.append(c[0]) - e.append(''.join(d)) - return(bytes(str(e[0]).encode())) +from typing import Any +class Hashing(): + def __init__(self) -> None: + self.salt = None + def __call__(self, *args:Any): + self.salt = args[0] + def __str__(self) -> str: + return "Hashing Funcitions In Here" -def _Salt(data): - salt = [] - salt.append(str(hashlib.sha256(uuid.uuid4().bytes).digest()).split("'")[1]) - salt.append(str(data).split("'")[1]) - salt.append(str(hashlib.sha256(uuid.uuid4().bytes).digest()).split("'")[1]) - return bytes(''.join(salt).encode()) - -def SHA256(data:str): - sha = hashlib.sha256(bytes(data.encode())) - hash = sha.digest() - return _Salt(hash) + def Standard_Multi_Hash(self,Data:str): + '''Inreversable Salted Hash Function Don't Use If U Want To Get The Content Back''' + a = hashlib.sha256(); a.update(bytes(Data.encode())); b = [] + base = hashlib.sha512() + md = hashlib.md5() + b.append(str(a.digest()).split("'")[1]) + b[0] = str(base64.urlsafe_b64encode(bytes(b[0].encode()))).split("'")[1] + base.update(bytes(b[0].encode())) + md.update(base.digest()) + b[0]=str(base64.urlsafe_b64encode(base64.standard_b64encode(md.digest()))).split("'")[1] + salt = ['H', 'c', 'D', 'L', 'b', 'M', 'S', 'a', 'N', 'q', 'K', 'j', 'V', 'd', 'O', 'W', 'x'] + c = (b[0].split("G"))or(b[0].split("g"))or(b[0].split("v"))or(b[0].split("x")); d=[]; e=[] + for i in range(len(c)): a = salt[i]; b = c[i]; c[i] = b+a + for i in range(len(c)): + try: d.append(c[i+1]) + except: d.append(c[0]) + e.append(''.join(d)) + return(bytes(str(e[0]).encode())) -def SHA512(data:str): - sha = hashlib.sha512(bytes(data.encode())) - hash = str(sha.digest()) - return _Salt(hash) + def __Salt(self,data,salt:bytes = None): + if not salt: + salts = [] + salts.append(str(hashlib.sha256(uuid.uuid4().bytes).digest()).split("'")[1]) + salts.append(str(data).split("'")[1]) + salts.append(str(hashlib.sha256(uuid.uuid4().bytes).digest()).split("'")[1]) + salts.append(str(hashlib.sha256(uuid.uuid4().bytes).digest()).split("'")[1]) + salts.append(str(data).split("'")[1]) + salts.append(str(data).split("'")[1]) + salts.append(str(hashlib.sha256(uuid.uuid4().bytes).digest()).split("'")[1]) + return base64.standard_b64encode(bytes((str(base64.urlsafe_b64encode(bytes(''.join(salts).encode()))).split("'")[1]+str(base64.urlsafe_b64encode(base64.standard_b64encode((bytes(''.join(salts).encode()))))).split("'")[1]).encode())) + if salt: + salts = [] + salts.append(str(hashlib.sha256(salt).digest()).split("'")[1]) + salts.append(str(data).split("'")[1]) + salts.append(str(hashlib.sha256(salt).digest()).split("'")[1]) + salts.append(str(hashlib.sha256(salt).digest()).split("'")[1]) + salts.append(str(data).split("'")[1]) + salts.append(str(data).split("'")[1]) + salts.append(str(hashlib.sha256(salt).digest()).split("'")[1]) + return base64.standard_b64encode(bytes((str(base64.urlsafe_b64encode(bytes(''.join(salts).encode()))).split("'")[1]+str(base64.urlsafe_b64encode(base64.standard_b64encode((bytes(''.join(salts).encode()))))).split("'")[1]).encode())) + + def SHA256(self,data:str): + sha = hashlib.sha256(bytes(data.encode())) + hash = sha.digest() + return self.__Salt(hash,salt=self.salt) -def SHA244(data:str): - sha = hashlib.sha224(bytes(data.encode())) - hash = str(sha.digest()) - return _Salt(hash) + def SHA512(self,data:str): + sha = hashlib.sha512(bytes(data.encode())) + hash = str(sha.digest()) + return self.__Salt(hash,salt=self.salt) -def MD5(data:str): - sha = hashlib.md5(bytes(data.encode())) - hash = str(sha.digest()) - return _Salt(hash) + def SHA244(self,data:str): + sha = hashlib.sha224(bytes(data.encode())) + hash = str(sha.digest()) + return self.__Salt(hash,salt=self.salt) -def SHA384(data:str): - sha = hashlib.sha384(bytes(data.encode())) - hash = str(sha.digest()) - return _Salt(hash) + def MD5(self,data:str): + sha = hashlib.md5(bytes(data.encode())) + hash = str(sha.digest()) + return self.__Salt(hash,salt=self.salt) -def BLAKE2(data:bytes): - a = hashes.Hash(hashes.BLAKE2s(32)) - a.update(data) - return _Salt(a.finalize()) - \ No newline at end of file + def SHA384(self,data:str): + sha = hashlib.sha384(bytes(data.encode())) + hash = str(sha.digest()) + return self.__Salt(hash,salt=self.salt) + + def BLAKE2(self,data:bytes): + a = hashes.Hash(hashes.BLAKE2s(32)) + a.update(data) + return self.__Salt(a.finalize(),salt=self.salt) + \ No newline at end of file