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.2.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.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nonces-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 8521e0b377f298f30ae7e0f67bb6f39e1bc2fe86ec78ad8206aa0f6cf2c148cc
MD5 4cf3a2b7b3f3d9304816b1013f1bf252
BLAKE2b-256 ca8e91015b9867dd7a0ec61393afaccb87f92863c53951e1ad6035c351874048

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nonces-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1ea82a45558515655cbdfa3dbcd6a7baf363e81c3c4026253c41e46a905cdfb7
MD5 ea163a6457d9d3b81dade1aec9bf02d3
BLAKE2b-256 3ce5e3c4203452d321a0931bea432056f2f92f4858d9795bee9671698fa8c590

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