hbutils.random.string
This module provides utilities for generating random strings in various formats.
It includes functions for creating random digits in different bases (binary, decimal, hexadecimal), random hash strings (MD5, SHA1), random base64 strings, and timestamp-prefixed hash strings. All functions support custom random number generators for reproducibility.
- The module exports the following functions:
random_digits: Generate random digits in any base from 2 to 36
random_bin_digits: Generate random binary digits
random_hex_digits: Generate random hexadecimal digits
random_md5: Generate random MD5 hash string
random_sha1: Generate random SHA1 hash string
random_base64: Generate random base64 encoded string
random_md5_with_timestamp: Generate random MD5 hash with timestamp prefix
random_sha1_with_timestamp: Generate random SHA1 hash with timestamp prefix
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
Nonewhich means just use the default one provided by system.
- Returns:
Random digital string.
- Return type:
str
- 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
Nonewhich 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
Nonewhich 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
Nonewhich 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
Nonewhich 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
Nonewhich 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
Nonewhich 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
Nonewhich 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'