Skip to main content

pyrandstring

Random string generation (passwords, identifiers) backed by a cryptographically secure source from the standard library.

Works on Python 2.7 and 3.x. On Python 3.6+ it uses secrets.choice; on earlier versions it falls back to random.SystemRandom, which draws from os.urandom - the same source secrets uses internally. The global random generator, which is predictable, is never used.

Installation

pip install pyrandstring

Usage

from pyrandstring import pyrandstring

p = pyrandstring()

p.getString()              # 18 characters from the full alphabet
p.getString(32)            # 32 characters from the full alphabet
p.getString(12, 'anum')    # 12 alphanumeric characters
p.getStringUnique()        # '1706462498_Xk2pQm9WvBn4TzLc'
p.getUUID()                # '4e9abb2e-9bcc-40c7-bd6f-3021c5e3a0ad'
p.getStringList(3, 12)     # ['6nINov%F(W6s', ',m4AwzBPlPe2', 'AeV7SNGclLVM']

Defaults can be set per instance:

p = pyrandstring()
p.length = 32
p.seed = 'bash'

p.getString()              # 32 characters, 'bash' alphabet

Passing length or seed to getString() affects that call only; the instance defaults are left untouched.

Alphabets

seed Characters Size
abc A-Z a-z 52
num 0-9 10
anum A-Z a-z 0-9 62
all anum + !"#$%&()*+,-./{|}~@ 81
bash all without " ( ) 78
zsh alias of bash 78

all is the default alphabet. bash omits the symbols that need escaping when the string is passed through a shell; zsh is an alias for it.

An unknown seed raises KeyError.

API

getString(length=None, seed=None)

Returns a random string of length length. When omitted, uses self.length (18) and self.seed ('all').

getStringUnique()

Returns <unix_timestamp>_<16 alphanumeric characters>. Intended for filenames and keys that also need to sort by time.

getUUID()

Returns a random UUID (version 4) as a string.

getStringList(quantity, length=None, seed=None)

Returns a list of quantity random strings. length and seed behave exactly as in getString(), and the instance defaults are left untouched.

p.getStringList(5, 16, 'anum')

Tests

python -m unittest discover tests -v

License

GPL-3.0. See LICENSE.txt.

Download files

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

Source Distribution

pyrandstring-0.1.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

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

pyrandstring-0.1.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file pyrandstring-0.1.0.tar.gz.

File metadata

  • Download URL: pyrandstring-0.1.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.5

File hashes

Hashes for pyrandstring-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f8e57e1bed133957a8bedc999504c3fb0bf83897dfb1bd74402966dd67f83688
MD5 d7d1e59ceb6b5d2e6f2522d93edc88c7
BLAKE2b-256 87820a4bd5f6d23d88ad7c0f160c1afae121170d4cfd7dca101e1d4b7db590b2

See more details on using hashes here.

File details

Details for the file pyrandstring-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyrandstring-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.5

File hashes

Hashes for pyrandstring-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f6d815a062af73c91a0443a5e00c4470f9ec4ea6107aadd12df30af0373bfbf
MD5 1036f5d04ef023d7c90fdc031de47293
BLAKE2b-256 480b3a7a53a3cb848530436498d335aa5452506ddc8eaeec440d7129150a15c8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

0.0.6

1 file

0.0.5

1 file

0.0.4

1 file

0.0.3

1 file

0.0.2

1 file

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