def Scrypt(password: Union[bytes, str], salt: Union[bytes, str], key_len: int, n: int, r: int, p: int) -> bytes: """ Compute the scrypt of the specified password, using the specified parameters. Args: password (str or bytes): Password salt (str or bytes) : Salt key_len (int) : Length of the derived key n (int) : CPU/Memory cost parameter r (int) : Block size parameter p (int) : Parallelization parameter Returns: bytes: Computed scrypt """ # Type for password and salt should be Union[bytes, str] in pycryptodome but it's only str, # so we ignore the mypy warning return scrypt( AlgoUtils.Encode(password), # type: ignore AlgoUtils.Encode(salt), # type: ignore key_len=key_len, N=n, r=r, p=p)
def Pbkdf2HmacSha512(password: Union[bytes, str], salt: Union[bytes, str], itr_num: int, dklen: Optional[int] = None) -> bytes: """ Compute the PBKDF2 HMAC-SHA512 of the specified password, using the specified keys and iteration number. Args: password (str or bytes): Password salt (str or bytes) : Salt itr_num (int) : Iteration number dklen (int, optional) : Length of the derived key (default: SHA-512 output length) Returns: bytes: Computed PBKDF2 HMAC-SHA512 """ if HASHLIB_USE_PBKDF2_SHA512: return hashlib.pbkdf2_hmac("sha512", AlgoUtils.Encode(password), AlgoUtils.Encode(salt), itr_num, dklen) # Use Cryptodome if not implemented in hashlib return PBKDF2( AlgoUtils.Encode(password), # type: ignore AlgoUtils.Encode(salt), dklen or SHA512.digest_size, count=itr_num, hmac_hash_module=SHA512)
def Sha512_256(data: Union[bytes, str]) -> bytes: """ Compute the SHA512/256 of the specified bytes. Args: data (str or bytes): Data Returns: bytes: Computed SHA512/256 """ if HASHLIB_USE_SHA512_256: return hashlib.new("sha512_256", AlgoUtils.Encode(data)).digest() # Use Cryptodome if not implemented in hashlib h = SHA512.new(truncate="256") h.update(AlgoUtils.Encode(data)) return h.digest()
def HmacSha256(key: Union[bytes, str], data: Union[bytes, str]) -> bytes: """ Compute the HMAC-SHA256 of the specified bytes with the specified key. Args: key (str or bytes) : Key data (str or bytes): Data Returns: bytes: Computed HMAC-SHA256 """ # Use digest if available if hasattr(hmac, "digest"): return hmac.digest(AlgoUtils.Encode(key), AlgoUtils.Encode(data), "sha256") return hmac.new(AlgoUtils.Encode(key), AlgoUtils.Encode(data), hashlib.sha256).digest()
def __init__(self, key: Union[str, bytes]) -> None: """ Construct class. Args: key (str or bytes): AES key """ self.aes = AES.new(AlgoUtils.Encode(key), AES.MODE_ECB) self.auto_unpad = True
def Sha256(data: Union[bytes, str]) -> bytes: """ Compute the SHA256 of the specified bytes. Args: data (str or bytes): Data Returns: bytes: Computed SHA256 """ return hashlib.sha256(AlgoUtils.Encode(data)).digest()
def Crc32(data: Union[bytes, str]) -> int: """ Compute the CRC32 of the specified bytes. Args: data (str or bytes): Data Returns: int: Computed CRC32 """ return binascii.crc32(AlgoUtils.Encode(data))
def FromHexString(data: Union[bytes, str]) -> bytes: """ Convert hex string to bytes. Args: data (str or bytes): Data bytes Returns bytes: Hex string converted to bytes """ return binascii.unhexlify(AlgoUtils.Encode(data))
def FromBinaryStr(data: Union[bytes, str]) -> int: """ Convert the specified binary string to integer. Args: data (str or bytes): Data Returns: int: Integer representation """ return int(AlgoUtils.Encode(data), 2)
def Pad(data: Union[str, bytes]) -> bytes: """ Pad data using PKCS7 algorithm. Args: data (str or bytes): Data to be padded Returns: bytes: Padded data """ return pad(AlgoUtils.Encode(data), AES.block_size)
def Kekkak256(data: Union[bytes, str]) -> bytes: """ Compute the Kekkak256 of the specified bytes. Args: data (str or bytes): Data Returns: bytes: Computed Kekkak256 """ h = keccak.new(digest_bits=256) h.update(AlgoUtils.Encode(data)) return h.digest()
def XModemCrc(data: Union[bytes, str]) -> bytes: """ Compute the XMODEM-CRC of the specified bytes. Args: data (str or bytes): Data Returns: bytes: Computed XMODEM-CRC """ crc_fct = crcmod.predefined.Crc("xmodem") crc_fct.update(AlgoUtils.Encode(data)) return crc_fct.digest()
def Ripemd160(data: Union[bytes, str]) -> bytes: """ Compute the RIPEMD-160 of the specified bytes. Args: data (str or bytes): Data Returns: bytes: Computed RIPEMD-160 """ h = RIPEMD160.new() h.update(AlgoUtils.Encode(data)) return h.digest()
def Encrypt(self, data: Union[str, bytes]) -> bytes: """ Encrypt data using AES-ECB algorithm. Args: data (str or bytes): Data to be encrypted Returns: bytes: Encrypted data """ padded_data = self.Pad(data) if self.auto_pad else AlgoUtils.Encode( data) return self.aes.encrypt(padded_data)
def Encode(data: Union[bytes, str], custom_alphabet: Optional[str] = None) -> str: """ Encode to Base32. Args: data (str or bytes) : Data custom_alphabet (str, optional): Custom alphabet string Returns: str: Encoded string """ b32_enc = AlgoUtils.Decode(base64.b32encode(AlgoUtils.Encode(data))) if custom_alphabet is not None: b32_enc = _Base32Utils.TranslateAlphabet(b32_enc, Base32Const.ALPHABET, custom_alphabet) return b32_enc
def Blake2b(data: Union[bytes, str], digest_size: int = 64, key: bytes = b"", salt: bytes = b"") -> bytes: """ Compute the Blake2b of the specified bytes. Args: data (str or bytes) : Data digest_size (int, optional): Digest size (default: 64) key (bytes, optional) : Key bytes (default: empty) salt (bytes, optional) : Salt bytes (default: empty) Returns: bytes: Computed Blake2b """ return hashlib.blake2b(AlgoUtils.Encode(data), digest_size=digest_size, key=key, salt=salt).digest()