Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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()
Ejemplo n.º 9
0
    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))
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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))
Ejemplo n.º 12
0
    def ToHexString(data_bytes: bytes, encoding: str = "utf-8") -> str:
        """
        Convert bytes to hex string.

        Args:
            data_bytes (bytes)      : Data bytes
            encoding (str, optional): Encoding type, utf-8 by default

        Returns:
            str: Bytes converted to hex string
        """
        return AlgoUtils.Decode(binascii.hexlify(data_bytes), encoding)
Ejemplo n.º 13
0
    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()
Ejemplo n.º 14
0
    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()
Ejemplo n.º 15
0
    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()
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
    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()