Module refinery.units.crypto.hash.checksums

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

Expand source code Browse git
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Implements hash algorithms of short length, commonly used as checksums.
"""
import zlib

from refinery.units.crypto.hash import HashUnit


class crc32(HashUnit):
    """
    Returns the CRC32 hash of the input data.
    """
    def _algorithm(self, data: bytes) -> bytes:
        return zlib.crc32(data).to_bytes(4, 'big')


class adler32(HashUnit):
    """
    Returns the Adler32 hash of the input data.
    """
    def _algorithm(self, data: bytes) -> bytes:
        return zlib.adler32(data).to_bytes(4, 'big')


class djb2(HashUnit):
    """
    Computes the DJB2 hash of the input data.
    """
    def _algorithm(self, data: bytes) -> bytes:
        h = 5381
        for b in data:
            h = ((h << 5) + h + b) & 0xFFFFFFFF
        return h.to_bytes(4, 'big')

Classes

class crc32 (reps=1, text=False)

Returns the CRC32 hash of the input data.

Expand source code Browse git
class crc32(HashUnit):
    """
    Returns the CRC32 hash of the input data.
    """
    def _algorithm(self, data: bytes) -> bytes:
        return zlib.crc32(data).to_bytes(4, 'big')

Ancestors

Class variables

var required_dependencies
var optional_dependencies
var console
var reverse

Inherited members

class adler32 (reps=1, text=False)

Returns the Adler32 hash of the input data.

Expand source code Browse git
class adler32(HashUnit):
    """
    Returns the Adler32 hash of the input data.
    """
    def _algorithm(self, data: bytes) -> bytes:
        return zlib.adler32(data).to_bytes(4, 'big')

Ancestors

Class variables

var required_dependencies
var optional_dependencies
var console
var reverse

Inherited members

class djb2 (reps=1, text=False)

Computes the DJB2 hash of the input data.

Expand source code Browse git
class djb2(HashUnit):
    """
    Computes the DJB2 hash of the input data.
    """
    def _algorithm(self, data: bytes) -> bytes:
        h = 5381
        for b in data:
            h = ((h << 5) + h + b) & 0xFFFFFFFF
        return h.to_bytes(4, 'big')

Ancestors

Class variables

var required_dependencies
var optional_dependencies
var console
var reverse

Inherited members