Skip to main content

Simple cryptography library

Project description

Simple cryptography library

> pip install kry

BEWARE: This library (currently) is not optimized and is probably vulnerable to side channel attacks.

Sections
1. Introduction
2. API reference
2.1 Hash functions
3. Examples
4. Changelog

1. Introduction

Features
* Simple: one operation (e.g. SHA-256 hashing) is one function (e.g. kry.sha256("hello")).
* Data streaming using generator.
* Save/Restore state.
* Save: an empty yield returns the current state (as bytes).
* Restore: initial_state parameter.

2. API reference

The library (currently) only implements hash functions (see section 2.1).

kry.__all__ is a tuple containing all public names in the library.

2.1 Hash functions

md2, md4, md5, ripemd128, ripemd160, ripemd256, ripemd320, sha1, sha224, sha256, sha384, sha512,
sha512_224, sha512_256, sm3.

Return: Bytes.

Parameters: (data, initial_state=None)

* data: Data to be hashed. Must be string, bytes, iterable (of strings and/or bytes), .read()-able (of
strings and/or bytes) or pathlib.Path (to a file). Strings will be UTF-8-encoded.
* initial_state: Must be bytes or None. If data is a generator, an empty yield returns the current
state (as bytes).

3. Examples

import kry

# string
kry.sha256("") # b"\xda9\xa3\xee^kK\r2U\xbf...
kry.sha256("").hex() # "da39a3ee5e6b4b0d3255bf...
kry.sha256("abc")

# bytes
kry.sha256(b"\x00\x01\x02-hello\x03")
kry.sha256(kry.sha256("double hash"))

def gen(): # Python generator may be used for streaming data
yield "Red"
yield "Green"

# An empty yield returns the current state (as bytes)
state = yield
print(state) # Print SHA-256 state after "RedGreen" has been processed
kry.sha256("Yellow", initial_state=state) # Equivalent to kry.sha256("RedGreenYellow")

yield "Blue"

# iterable
kry.sha256(gen()) # Equivalent to kry.sha256("RedGreenBlue")
kry.sha256(["Red", b"Green", "Blue"]) # Equivalent to kry.sha256("RedGreenBlue")
kry.sha256(((i*i).to_bytes(1) for i in range(4))) # Equivalent to kry.sha256(b"\x00\x01\x04\x09")

# .read()-able
kry.sha256(open("red.txt"))
with open("blue.txt") as file:
kry.sha256(file)
kry.sha256(open("cat.png", "rb"))
import io
kry.sha256(io.BytesIO(b"12345"))

# pathlib.Path
from pathlib import Path
kry.sha256(Path("colors.txt"))
kry.sha256(Path("dog.gif"))

4. Changelog

1.0.0
* Add md2, md4, md5, ripemd128, ripemd160, ripemd256, ripemd320, sha1, sha224, sha256, sha384,
sha512, sha512_224, sha512_256, sm3.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kry-1.0.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kry-1.0.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file kry-1.0.0.tar.gz.

File metadata

  • Download URL: kry-1.0.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kry-1.0.0.tar.gz
Algorithm Hash digest
SHA256 dd644fc466ac6d45fc8e99f1c52bdafb788e12442e114d3371aa478509abb551
MD5 95161b396b0e45baf5033fc4e4e5080c
BLAKE2b-256 53c583969e74bb7092d10cde14d41003b93630cf1f25fcb44975405e7ae70883

See more details on using hashes here.

Provenance

The following attestation bundles were made for kry-1.0.0.tar.gz:

Publisher: pypi-publish.yml on 0b-0/kry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kry-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: kry-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kry-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3cfb849cc9df7a367885ef09a31cda16a07119501e266ae8c78ac8af027875d6
MD5 a68c7a8c9aaf595ca4a0ccfd0bb58fa2
BLAKE2b-256 ddff592a5580f1598c13c0e840e815cff0243d6041bfe3f25751292a714ce1df

See more details on using hashes here.

Provenance

The following attestation bundles were made for kry-1.0.0-py3-none-any.whl:

Publisher: pypi-publish.yml on 0b-0/kry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page