Skip to main content

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

Metadata

Release files for nonces 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nonces 1.0.3
File Size Uploaded
nonces-1.0.3.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nonces 1.0.3
File Interpreter ABI Platform
nonces-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / nonces-1.0.3.tar.gz

Download URL nonces-1.0.3.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f8b6e890577a1947cab3a82eb4602bd1db908fbcffc0dc6eee8443bfcfdb44dc
BLAKE2b-256 checksum
How to use checksums
b8333640df5d5b5777001d7f5b52368ac0da5e2498dd921f52a2f836f4db1d63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release files / nonces-1.0.3-py3-none-any.whl

Download URL nonces-1.0.3-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e25b3288b519124b9404356915f048f1afcc28f39db5845d33719678f0dd4c9
BLAKE2b-256 checksum
How to use checksums
21d07656420c0268a5aa26ab544c1e51f31c079c6f1938ec68ff76efd7e2453d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page