Skip to content

Commit

Permalink
Re-written EnroCrypt In OOP
Browse files Browse the repository at this point in the history
* Added core.py
* Added The Option Of Custom Salt
* warped Everything In Classes
Now Just Import Core from enrocrypt.core and User Can Use All Functions.
  • Loading branch information
metamorphic-spyware committed Jun 29, 2021
1 parent 4225db6 commit efe3e66
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 212 deletions.
100 changes: 52 additions & 48 deletions enrocrypt/basic.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions enrocrypt/core.py
Original file line number Diff line number Diff line change
@@ -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

229 changes: 117 additions & 112 deletions enrocrypt/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Loading

0 comments on commit efe3e66

Please sign in to comment.