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.3.tar.gz (8.6 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.3-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nonces-1.0.3.tar.gz
  • Upload date:
  • Size: 8.6 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.3.tar.gz
Algorithm Hash digest
SHA256 f8b6e890577a1947cab3a82eb4602bd1db908fbcffc0dc6eee8443bfcfdb44dc
MD5 1c091cd27d5867e14cdec8c88ad98688
BLAKE2b-256 b8333640df5d5b5777001d7f5b52368ac0da5e2498dd921f52a2f836f4db1d63

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nonces-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.9 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0e25b3288b519124b9404356915f048f1afcc28f39db5845d33719678f0dd4c9
MD5 c8835f74717398d6890d10562b1a4e1e
BLAKE2b-256 21d07656420c0268a5aa26ab544c1e51f31c079c6f1938ec68ff76efd7e2453d

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