hbutils.random.string

Random string generation utilities for hashes, digits, and timestamps.

This module offers convenience helpers for producing random strings in various formats, including base-N digit strings, cryptographic hash strings, base64-encoded strings, and timestamp-prefixed hashes. Each public function accepts an optional random.Random instance to enable deterministic output for testing or reproducibility.

The module provides the following public utilities:

Note

The random hash helpers generate random bytes first and then apply hashing or encoding. They are intended for unique identifiers rather than cryptographically secure tokens.

Example:

>>> from hbutils.random import random_digits, random_sha1_with_timestamp
>>> random_digits(length=8, base=16)
'3af27b9c'
>>> random_sha1_with_timestamp()
'20220116233121916685_fba840b80163b55cd2295d84286a438bf8acb7c0'

__all__

hbutils.random.string.__all__ = ['random_digits', 'random_bin_digits', 'random_hex_digits', 'random_md5', 'random_sha1', 'random_base64', 'random_md5_with_timestamp', 'random_sha1_with_timestamp']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

random_digits

hbutils.random.string.random_digits(length: int = 32, base: int = 10, upper: bool = False, rnd: Random | None = None) str[source]

Create random digits in the specified base.

Parameters:
  • length (int) – Length of the digits, default is 32.

  • base (int) – Base of the digits, should be in [2, 36], default is 10.

  • upper (bool) – Upper the hex chars, default is False.

  • rnd (Optional[random.Random]) – Random object you used, default is None which means just use the default one provided by system.

Returns:

Random digital string.

Return type:

str

Raises:
  • ValueError – If base is less than 2 or greater than 36.

  • TypeError – If base is not an integer.

Examples::
>>> from hbutils.random import random_digits
>>> random_digits()
'53518555004529024184262875530824'
>>> random_digits(base=8)
'77337055655313664176450107031511'
>>> random_digits(48, base=8)
'130107050101775254773050732461131017135371516420'

random_bin_digits

hbutils.random.string.random_bin_digits(length: int = 32, rnd: Random | None = None) str[source]

Create random binary digits.

Parameters:
  • length (int) – Length of the digits, default is 32.

  • rnd (Optional[random.Random]) – Random object you used, default is None which means just use the default one provided by system.

Returns:

Random binary digital string.

Return type:

str

Examples::
>>> from hbutils.random import random_bin_digits
>>> random_bin_digits()
'11001011010101101100011010010011'
>>> random_bin_digits(48)
'010110000110101101111111011100010011101011010100'

random_hex_digits

hbutils.random.string.random_hex_digits(length: int = 32, upper: bool = False, rnd: Random | None = None) str[source]

Create random hexadecimal digits.

Parameters:
  • length (int) – Length of the digits, default is 32.

  • upper (bool) – Whether to use uppercase letters, default is False.

  • rnd (Optional[random.Random]) – Random object you used, default is None which means just use the default one provided by system.

Returns:

Random hexadecimal digital string.

Return type:

str

Examples::
>>> from hbutils.random import random_hex_digits
>>> random_hex_digits()
'bf4eadfb8c1700d74024833c3ce211a7'
>>> random_hex_digits(upper=True)
'7B85DE69A319BA132ACA27C7777A1C3E'
>>> random_hex_digits(48)
'7175a23730391687b7b5230c72d702a1664833a1c66783cc'

random_md5

hbutils.random.string.random_md5(rnd: Random | None = None) str[source]

Create random md5 string.

Parameters:

rnd (Optional[random.Random]) – Random object you used, default is None which means just use the default one provided by system.

Returns:

Random md5 string.

Return type:

str

Examples::
>>> from hbutils.random import random_md5
>>> random_md5()
'bbffd8913a7c49161ebe31b9092a9016'

random_sha1

hbutils.random.string.random_sha1(rnd: Random | None = None) str[source]

Create random sha1 string.

Parameters:

rnd (Optional[random.Random]) – Random object you used, default is None which means just use the default one provided by system.

Returns:

Random sha1 string.

Return type:

str

Examples::
>>> from hbutils.random import random_sha1
>>> random_sha1()
'13135aa6b05482dcdbc1f5a25d117298571e7fab'

random_base64

hbutils.random.string.random_base64(length: int = 64, rnd: Random | None = None) str[source]

Create random base64 string, may be useful when matrix verification code.

Parameters:
  • length (int) – Length of the original binary data, default is 64.

  • rnd (Optional[random.Random]) – Random object you used, default is None which means just use the default one provided by system.

Returns:

Random base64 string.

Return type:

str

Examples::
>>> from hbutils.random import random_base64
>>> random_base64()
'PJZzHkM2-DpeXn1W9b3rp0I66MnOeD-31d2XYTA3va7N8DSNmQgvIINnvDMKWaRW-WHo_ftgKHg40z7XbDupbg=='
>>> random_base64(48)
'siRZNSeytSUXlIgKYuZOzbhehhI7oabcxFDB07PkjyZ5b0DI5hGC0pqjJFlD6NGQ'

random_md5_with_timestamp

hbutils.random.string.random_md5_with_timestamp(rnd: Random | None = None) str[source]

Create random md5 string with timestamp prefix.

The format is ‘{timestamp}_{md5}’ where timestamp is in ‘YYYYMMDDHHMMSSffffff’ format.

Parameters:

rnd (Optional[random.Random]) – Random object you used, default is None which means just use the default one provided by system.

Returns:

Random md5 string with timestamp prefix.

Return type:

str

Examples::
>>> from hbutils.random import random_md5_with_timestamp
>>> random_md5_with_timestamp()
'20220116233104357175_daf67fde4b758ff4aae21cc77f5ed689'

random_sha1_with_timestamp

hbutils.random.string.random_sha1_with_timestamp(rnd: Random | None = None) str[source]

Create random sha1 string with timestamp prefix.

The format is ‘{timestamp}_{sha1}’ where timestamp is in ‘YYYYMMDDHHMMSSffffff’ format.

Parameters:

rnd (Optional[random.Random]) – Random object you used, default is None which means just use the default one provided by system.

Returns:

Random sha1 string with timestamp prefix.

Return type:

str

Examples::
>>> from hbutils.random import random_sha1_with_timestamp
>>> random_sha1_with_timestamp()
'20220116233121916685_fba840b80163b55cd2295d84286a438bf8acb7c0'