Module refinery.units.crypto.hash.cryptographic

Implements various cryptographic hashing algorithms.

Expand source code Browse git
"""
Implements various cryptographic hashing algorithms.
"""
from __future__ import annotations

import hashlib

from typing import TYPE_CHECKING, Callable

from refinery.lib.tools import normalize_to_display
from refinery.units import Executable
from refinery.units.crypto.hash import HashUnit

if TYPE_CHECKING:
    from refinery.lib.types import buf
    from refinery.units.crypto.keyderive import _Hash


def _doc(name: str):
    return F'Returns the {normalize_to_display(name.upper())} hash of the input data.'


class _HashExe(Executable):
    _build_hash: Callable[[buf], _Hash]

    def _algorithm(cls, data: buf):
        return cls._build_hash(data).digest()

    def __new__(cls, name: str, bases, namespace: dict, kernel: str = ''):
        kernel = kernel or name
        namespace.update(__doc__=_doc(kernel), _algorithm=cls._algorithm)
        exe = Executable.__new__(cls, name, bases, namespace)
        setattr(exe, '__qualname__', name)
        return exe


class _CDome(_HashExe):
    def __init__(cls, name: str, bases, nmspc: dict, kernel: str = '', **kw):
        kernel = kernel or name.upper()

        super().__init__(name, bases, nmspc, **kw)

        def _build_hash(data: buf) -> _Hash:
            hash = __import__(F'Cryptodome.Hash.{kernel}')
            for t in ('Hash', kernel, 'new'):
                hash = getattr(hash, t)
            return hash(data)

        cls._build_hash = staticmethod(_build_hash)


class _PyLib(_HashExe):
    def __init__(cls, name: str, bases, nmspc: dict, kernel: str = '', **kw):
        kernel = kernel or name
        super().__init__(name, bases, nmspc, **kw)
        cls._build_hash = getattr(hashlib, kernel)


class ripemd160 (HashUnit, metaclass=_CDome)                     : ...  # noqa
class md2       (HashUnit, metaclass=_CDome)                     : ...  # noqa
class md4       (HashUnit, metaclass=_CDome)                     : ...  # noqa
class keccak    (HashUnit, metaclass=_CDome, kernel='keccak')    : ...  # noqa
class md5       (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha1      (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha224    (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha256    (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha384    (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha512    (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class blake2b   (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class blake2s   (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha3_224  (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha3_256  (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha3_384  (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class sha3_512  (HashUnit, metaclass=_PyLib)                     : ...  # noqa
class shake128  (HashUnit, metaclass=_PyLib, kernel='shake_128') : ...  # noqa
class shake256  (HashUnit, metaclass=_PyLib, kernel='shake_256') : ...  # noqa


class ripemd128(HashUnit):
    """
    Returns the RIPEMD-128 hash of the input data.
    """
    def _algorithm(self, data):
        from refinery.lib.crypto.ripemd128 import ripemd128
        return ripemd128(data)

Classes

class ripemd160 (reps=1, text=False)

Returns the RIPEMD160 hash of the input data.

Expand source code Browse git
class ripemd160 (HashUnit, metaclass=_CDome)                     : ...  # noqa

Ancestors

Subclasses

Class variables

var reverse

The type of the None singleton.

Inherited members

class md2 (reps=1, text=False)

Returns the MD2 hash of the input data.

Expand source code Browse git
class md2       (HashUnit, metaclass=_CDome)                     : ...  # noqa

Ancestors

Subclasses

Class variables

var reverse

The type of the None singleton.

Inherited members

class md4 (reps=1, text=False)

Returns the MD4 hash of the input data.

Expand source code Browse git
class md4       (HashUnit, metaclass=_CDome)                     : ...  # noqa

Ancestors

Subclasses

Class variables

var reverse

The type of the None singleton.

Inherited members

class keccak (reps=1, text=False)

Returns the KECCAK hash of the input data.

Expand source code Browse git
class keccak    (HashUnit, metaclass=_CDome, kernel='keccak')    : ...  # noqa

Ancestors

Subclasses

Class variables

var reverse

The type of the None singleton.

Inherited members

class md5 (reps=1, text=False)

Returns the MD5 hash of the input data.

Expand source code Browse git
class md5       (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha1 (reps=1, text=False)

Returns the SHA1 hash of the input data.

Expand source code Browse git
class sha1      (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha224 (reps=1, text=False)

Returns the SHA224 hash of the input data.

Expand source code Browse git
class sha224    (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha256 (reps=1, text=False)

Returns the SHA256 hash of the input data.

Expand source code Browse git
class sha256    (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha384 (reps=1, text=False)

Returns the SHA384 hash of the input data.

Expand source code Browse git
class sha384    (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha512 (reps=1, text=False)

Returns the SHA512 hash of the input data.

Expand source code Browse git
class sha512    (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class blake2b (reps=1, text=False)

Returns the BLAKE2B hash of the input data.

Expand source code Browse git
class blake2b   (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class blake2s (reps=1, text=False)

Returns the BLAKE2S hash of the input data.

Expand source code Browse git
class blake2s   (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha3_224 (reps=1, text=False)

Returns the SHA3-224 hash of the input data.

Expand source code Browse git
class sha3_224  (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha3_256 (reps=1, text=False)

Returns the SHA3-256 hash of the input data.

Expand source code Browse git
class sha3_256  (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha3_384 (reps=1, text=False)

Returns the SHA3-384 hash of the input data.

Expand source code Browse git
class sha3_384  (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class sha3_512 (reps=1, text=False)

Returns the SHA3-512 hash of the input data.

Expand source code Browse git
class sha3_512  (HashUnit, metaclass=_PyLib)                     : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class shake128 (reps=1, text=False)

Returns the SHAKE-128 hash of the input data.

Expand source code Browse git
class shake128  (HashUnit, metaclass=_PyLib, kernel='shake_128') : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class shake256 (reps=1, text=False)

Returns the SHAKE-256 hash of the input data.

Expand source code Browse git
class shake256  (HashUnit, metaclass=_PyLib, kernel='shake_256') : ...  # noqa

Ancestors

Class variables

var reverse

The type of the None singleton.

Inherited members

class ripemd128 (reps=1, text=False)

Returns the RIPEMD-128 hash of the input data.

Expand source code Browse git
class ripemd128(HashUnit):
    """
    Returns the RIPEMD-128 hash of the input data.
    """
    def _algorithm(self, data):
        from refinery.lib.crypto.ripemd128 import ripemd128
        return ripemd128(data)

Ancestors

Subclasses

Class variables

var reverse

The type of the None singleton.

Inherited members