Metadata-Version: 2.5
Name: sha256bit
Version: 0.2.4
Summary: SHA256/SHA224 with bit granularity for message input length
Project-URL: Homepage, https://github.com/sebastien-riou/sha256bit
Project-URL: Bug Tracker, https://github.com/sebastien-riou/sha256bit/issues
Author: Sebastien Riou
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=3.12
Description-Content-Type: text/markdown

# sha256bit


| | |
| --- | --- |
| CI/CD | [![CI - Test](https://github.com/sebastien-riou/sha256bit/actions/workflows/test.yml/badge.svg)](https://github.com/sebastien-riou/sha256bit/actions/workflows/test.yml) [![CD - Build](https://github.com/sebastien-riou/sha256bit/actions/workflows/build.yml/badge.svg)](https://github.com/sebastien-riou/sha256bit/actions/workflows/build.yml) [![Documentation Status](https://readthedocs.org/projects/sha256bit/badge/?version=latest)](https://sha256bit.readthedocs.io/en/latest/?badge=latest)|
| Package | [![PyPI - Version](https://img.shields.io/pypi/v/sha256bit.svg?logo=pypi&label=PyPI&logoColor=gold)](https://pypi.org/project/sha256bits/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sha256bit.svg?logo=python&label=Python&logoColor=gold)](https://pypi.org/project/sha256bit/) |
| Meta | [![Hatch project](https://img.shields.io/badge/%F0%9F%A5%9A-Hatch-4051b5.svg)](https://github.com/pypa/hatch)  [![linting - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v0.json)](https://github.com/charliermarsh/ruff) [![code style - Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![types - Mypy](https://img.shields.io/badge/types-Mypy-blue.svg)](https://github.com/python/mypy) [![License - apache-2.0](https://img.shields.io/badge/license-apache--2.0-blue)](https://spdx.org/licenses/) |


Pure python implementation of SHA256 and SHA224 with features which are often lacking:
- bit granularity for message input length
- import/export API to "persist" the state in the middle of a hash computation

[User documentation](https://sha256bit.rtfd.io) is hosted on readthedocs.

## Installation

### Command line tool
Install with [pipx](https://pipx.pypa.io), it puts the `sha256bit` command in its own isolated environment:

    pipx install sha256bit

The message is given as a hex string:

    $ sha256bit 616263
    BA 78 16 BF 8F 01 CF EA 41 41 40 DE 5D AE 22 23 B0 03 61 A3 96 17 7A 9C B4 10 FF 61 F2 00 15 AD
    $ sha256bit --algo sha224 616263
    23 09 7D 22 34 05 D8 22 86 42 A4 77 BD A2 55 B3 2A AD BC E4 BD A0 B3 F7 E3 6C 9D A7
    $ sha256bit --bit-length 1 00
    BD 4F 9E 98 BE B6 8C 6E AD 32 43 B1 B4 C7 FE D7 5F A4 FE AA B1 F8 47 95 CB D8 A9 86 76 A2 A3 75

If `pipx` is missing, on Debian/Ubuntu: `sudo apt install pipx && pipx ensurepath`.

### Python API
Add it to your project with [pipenv](https://pipenv.pypa.io):

    pipenv install sha256bit

If `pipenv` is missing: `pipx install pipenv`.

## Usage

### One liner 

    >>> from sha256bit import Sha256bit
    >>> Sha256bit("abc".encode()).hexdigest()
    'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'

### SHA-224
`Sha224bit` has the same API as `Sha256bit`.

    >>> from sha256bit import Sha224bit
    >>> Sha224bit("abc".encode()).hexdigest()
    '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7'

### Bit length capability

    >>> from sha256bit import Sha256bit
    >>> Sha256bit(b'\x00',bitlen=1).hexdigest()
    'bd4f9e98beb68c6ead3243b1b4c7fed75fa4feaab1f84795cbd8a98676a2a375'

### Import/export

    >>> from sha256bit import Sha256bit
    >>> h1 = Sha256bit("a".encode())
    >>> state = h1.export_state()
    >>> h2 = Sha256bit.import_state(state)
    >>> h2.update("bc".encode())
    >>> h2.hexdigest()
    'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'

## Test with `pytest`

    pytest-3

## Test without `pytest`
Tests can run without creating/installing the package:

    python3 -m test.test


you can also run each test separately:

    python3 -m test.test_api
    python3 -m test.test_cavp
    python3 -m test.test_hardcoded
    python3 -m test.test_vs_hashlib

## Generate the doc

    pipenv sync --dev
    pipenv run make -C docs clean doctest html

## Update pipenv

The root `Pipfile` holds the tools for building, linting and the doc, all in `[dev-packages]` since the package itself has no dependencies. CI and Read the Docs install the versions pinned in `Pipfile.lock`, so commit it after updating:

    pipenv update --dev
    
## Build the package
Build is done using `hatchling`.

    pipenv sync --dev
    pipenv run ./build


## Create a new version
Version is managed by `hatch-vcs`, you just need to create a tag in github. 

## Launch linters
They use the configuration from `pyproject.toml`

    pipenv sync --dev
    pipenv run ./lint

