Skip to main content

Nonces generator for cryptographic purposes

Project description

nonces

A nonces generator based on a given size, a counter size, an optional seed and an optional byte order for the counter.

Installation

pip install nonces

Usage

Basic Nonce Example:

from nonces import Nonce

# This will generate a random one-time 24 bytes nonce
nonce = Nonce.random(24)

Basic Nonces Example:

from nonces import Nonces

# This will initiate an 8 bytes nonce with a 4 bytes counter
nonces = Nonces(size=8, counter_size=4)

# By default the counter is big endian with a random seed
# and the counter trailing at the end of the full nonce bytes

# Get the current nonce
nonce = nonces.nonce

print(nonce)

# Update the current counter
nonce = nonces.update()

print(nonce)

Update counter:

# Set counter
nonce = nonces.set_counter(10)

print(nonce)

# Get the counter value
print(nonces.counter)

# Get the counter value in bytes
nonces.counter_bytes

Check the OverFlowError:

# Check the OverFlowError exception when counter runs out
nonces.set_counter(nonces.max_counter)

nonces.update()

Create nonce with counter from a specific seed:

# We can create a new object with the seed and change the byte order
# to little endian and a non-trailing counter (i.e, counter + nonce)

seed = b"\xff" * 4
nonces = Nonces(
   size=8,
   counter_size=4,
   seed=seed,
   order='little',
   trailing_counter=False
)
for i in range(10):
   nonces.update()

b'\x01\x00\x00\x00\xff\xff\xff\xff'
b'\x02\x00\x00\x00\xff\xff\xff\xff'
b'\x03\x00\x00\x00\xff\xff\xff\xff'
b'\x04\x00\x00\x00\xff\xff\xff\xff'
b'\x05\x00\x00\x00\xff\xff\xff\xff'
b'\x06\x00\x00\x00\xff\xff\xff\xff'
b'\x07\x00\x00\x00\xff\xff\xff\xff'
b'\x08\x00\x00\x00\xff\xff\xff\xff'
b'\t\x00\x00\x00\xff\xff\xff\xff'
b'\n\x00\x00\x00\xff\xff\xff\xff'

assert nonces.seed_bytes == seed

Set the increment value:


nonces = Nonces(size=8, counter_size=4, seed=seed)

nonces.increment = 255

for i in range(10):
   nonces.update()

b'\xff\xff\xff\xff\x00\x00\x00\xff'
b'\xff\xff\xff\xff\x00\x00\x01\xfe'
b'\xff\xff\xff\xff\x00\x00\x02\xfd'
b'\xff\xff\xff\xff\x00\x00\x03\xfc'
b'\xff\xff\xff\xff\x00\x00\x04\xfb'
b'\xff\xff\xff\xff\x00\x00\x05\xfa'
b'\xff\xff\xff\xff\x00\x00\x06\xf9'
b'\xff\xff\xff\xff\x00\x00\x07\xf8'
b'\xff\xff\xff\xff\x00\x00\x08\xf7'
b'\xff\xff\xff\xff\x00\x00\t\xf6'

Leverage bytes encoding options:

nonces = Nonces(size=8, counter_size=4)

nonce = nonces.nonce

nonce_hex = nonce.hex()

new_nonce = Nonce.fromhex(nonce_hex)

assert nonce == new_nonce

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

nonces-1.0.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

nonces-1.0.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file nonces-1.0.1.tar.gz.

File metadata

  • Download URL: nonces-1.0.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for nonces-1.0.1.tar.gz
Algorithm Hash digest
SHA256 188e4007a52d0ca816d61a319ff2e81d9c7329585e2cf27e59992e2d4124ca05
MD5 5b9165596c3c727973a6d6b27b4d0833
BLAKE2b-256 5f5ad4a2fb6609c5f75671b48926b4453b7af06ce7e3bf3ce47dfc1e129ab1dd

See more details on using hashes here.

File details

Details for the file nonces-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: nonces-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for nonces-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc27bb52518f91d2ee680630d3c8602d596609bad8590f7ee18898d10cc8a4fe
MD5 6046a6dad76c3804ffa46682c4ccd2fa
BLAKE2b-256 15ae9a06079a55222a0c8ccc82aeeb7827ac7468f96e0cd5343cb753a053d4dc

See more details on using hashes here.

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