Skip to content

Commit

Permalink
Small update for passwords.PasswordReGenerator as well as the start o…
Browse files Browse the repository at this point in the history
…f a rewrite of crypto
  • Loading branch information
adalfarus committed Jun 20, 2024
1 parent 6d51324 commit bc2c93b
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
93 changes: 93 additions & 0 deletions src/aplustools/security/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,99 @@
Kyber = KryptonKEM = Argon2 = None


# Bunch of enums to make it easier for the user
class Hash:
"""Provides an easy way for the user to specify a hash algorithm."""
SHA1 = 0
class SHA2:
"""SHA2"""
SHA224 = 1
SHA256 = 2
SHA384 = 3
SHA512 = 4
class SHA3:
"""SHA3"""
SHA224 = 5
SHA256 = 6
SHA384 = 7
SHA512 = 8
class BLAKE2:
"""BLAKE2"""
BLAKE2b = 9
BLAKE2s = 10
ARGON2 = 11
MD5 = 12
BCRYPT = 13


class Hasher:
def generate_hash(self, to_hash: bytes, algo: Hash):
return

def verify_hash(self, to_verify: bytes, hash, algo: Hash):
return


class SymCipher: # Symmetric Encryption
class AES: # Advanced Encryption Standard
AES256 = 0
ChaCha20 = 1
TripleDES = 2
Blowfish = 3
CASTS = 4


class SymOperation: # Modes of Operation
"""Different modes of operation"""
ECB = 0 # Electronic Codebook
CBC = 1 # Cipher Block Chaining
CFB = 2 # Cipher Feedback
OFB = 3 # Output Feedback
CTR = 4 # Counter
GCM = 5 # Galois/Counter Mode
OCB = 6 # Offset Codebook Mode


class ASymCipher: # Asymmetric Encryption
class RSA: # Rivest-Shamir-Adleman
RSA1024 = 0
DSA = 1 # Digital Signature Algorithm
class ECC: # Elliptic Curve Cryptography
ECDSA = 2 # Elliptic Curve Digital Signature Algorithm
ECDH = 3 # Elliptic Curve Diffie-Hellman
Ed25519 = 4
Ed448 = 5


class KeyDev: # Key Derivation Functions (KDFs)
PBKDF2 = 0 # Password-Based Key Derivation Function 2
Scrypt = 1
HKDF = 2 # HMAC-based Extract-and-Expand Key Derivation Function
X9dot63 = 3
X9dot42 = 4


class SymPadding: # Padding Schemes
PKCS7 = 0
ANSIX923 = 1
ISO7816 = 2


class ASymcPadding: # Asymmetric Encryption Padding Schemes
PKCShash1v1dot5 = 0 # Older padding scheme for RSA
OAEP = 1 # Optimal Asymmetric Encryption Padding
PSS = 2 # Probabilistic Signature Scheme


class AuthCodes: # Authentication Codes
HMAC = 0 # Hash-based Message Authentication Code
CMAC = 1 # Cipher-based Message Authentication Cod


class AdvancedCryptography:
pass


class CryptUtils:
@staticmethod
def pbkdf2(password: str, salt: bytes, length: int, cycles: int) -> bytes:
Expand Down
33 changes: 30 additions & 3 deletions src/aplustools/security/passwords.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import os.path

from cryptography.hazmat.primitives.ciphers import Cipher as _Cipher, algorithms as _algorithms, modes as _modes
from cryptography.hazmat.backends import default_backend as _default_backend
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC as _PBKDF2HMAC
from cryptography.hazmat.primitives import hashes as _hashes
from cryptography.hazmat.primitives import padding as _padding
from sympy import arg
from zxcvbn import zxcvbn as _zxcvbn

from aplustools.security.rand import WeightedRandom as _WeightedRandom
from aplustools.data import beautify_json as _beautify_json
from aplustools.io.environment import strict as _strict
from aplustools.security.crypto import CryptUtils

from typing import Union as _Union, Literal as _Literal, Optional as _Optional, Callable as _Callable
from aplustools.data import nice_number as _nice_number
Expand Down Expand Up @@ -1292,11 +1296,29 @@ def quick_big_reducer_3(input_str: str, ord_ranges: list[range]):
class PasswordReGenerator:
"""Create a secure password from a weak one plus an identifier like a website name. This is made possible by used
an encrypted seed file in combination with the PBKDF2HMAC algorithm (using 100_000 iterations and sha 256)."""
def __init__(self, key: bytes, seed_file: str = "seed_file.enc", debug: bool = False):
def __init__(self, key: bytes, seed_file_or_seed: str | bytes = "seed_file.enc", debug: bool = False,
_got_seed: bool = False):
self._key = key
self._seed_file = seed_file
self._seed_file = seed_file_or_seed
self._debug = debug
self._load_or_create_seed_file()

if not _got_seed:
self._load_or_create_seed_file()
else:
self._seed = self._seed_file
self._seed_file = None

@classmethod
def load_from_file(cls, file_path: str) -> "PasswordReGenerator":
"""Use a default file as the seed (hashed)."""
try:
with open(file_path, "r") as f:
contents = f.read()
except IOError:
with open(file_path, "rb") as f:
contents = f.read()
seed = CryptUtils.pbkdf2(contents, CryptUtils.generate_hash(file_path, "strong").encode(), 32, 1_000_000)
return cls(CryptUtils.generate_hash(os.path.basename(file_path), "strong").encode(), seed, False, True)

@staticmethod
def generate_suitable_password() -> bytes:
Expand Down Expand Up @@ -1389,6 +1411,11 @@ def _format_password(self, password: bytes, length: int, charset: _Literal["hex"
raise ValueError(f"Unsupported charset: {charset}")


generator = PasswordReGenerator.load_from_file(r"C:\Users\till_\OneDrive\Desktop\generate_password.bat")
print(generator.generate_password("Hello WOrld", "MyPass"))
input()


@_strict(mark_class_as_cover=False) # For security purposes
class SecurePasswordManager:
"""Securely stores passwords and gives various options to generate them too."""
Expand Down

0 comments on commit bc2c93b

Please sign in to comment.