Module refinery.units.crypto.hash

Implements various hashing algorithms.

Expand source code Browse git
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Implements various hashing algorithms.
"""
from refinery.units import arg, Unit, abc


class HashUnit(Unit, abstract=True):

    @abc.abstractmethod
    def _algorithm(self, data: bytes):
        raise NotImplementedError

    def __init__(self, text: arg('-t', help='Output a hexadecimal representation of the hash.') = False, **kwargs):
        super().__init__(text=text, **kwargs)

    def process(self, data: bytes) -> bytes:
        digest = self._algorithm(data)
        try:
            digest = digest.digest()
        except AttributeError:
            pass
        if self.args.text:
            digest = digest.hex().encode(self.codec)
        return digest

Sub-modules

refinery.units.crypto.hash.checksums

Implements hash algorithms of short length, commonly used as checksums.

refinery.units.crypto.hash.cryptographic

Implements various cryptographic hashing algorithms.

refinery.units.crypto.hash.imphash
refinery.units.crypto.hash.murmur
refinery.units.crypto.hash.xxhash

This code is based on a pure Python implementation of xxHash, sourced from the following GitHub repository: …

Classes

class HashUnit (text=False, **kwargs)
Expand source code Browse git
class HashUnit(Unit, abstract=True):

    @abc.abstractmethod
    def _algorithm(self, data: bytes):
        raise NotImplementedError

    def __init__(self, text: arg('-t', help='Output a hexadecimal representation of the hash.') = False, **kwargs):
        super().__init__(text=text, **kwargs)

    def process(self, data: bytes) -> bytes:
        digest = self._algorithm(data)
        try:
            digest = digest.digest()
        except AttributeError:
            pass
        if self.args.text:
            digest = digest.hex().encode(self.codec)
        return digest

Ancestors

Subclasses

Inherited members