Skip to content

Releases: cloud-rat/string-generator

v1.0.0

11 Mar 20:43

Choose a tag to compare

String Generator v1.0.0

Cryptographically-secure random string generator for PHP 8.2+ with zero production dependencies.

Highlights

  • Bit-packing with rejection sampling — uses random_bytes() for entropy
  • 7 predefined ASCII ranges — Numeric, Lowercase, Uppercase, Alpha, AlphaNumeric, Special, Any
  • Custom string alphabet — pass any string with ≥ 2 distinct bytes
  • Binary-safe — works with null bytes and full 0x00–0xFF range
  • PHPStan level 9 — strict static analysis, no baseline, no ignores

Usage

use Taranovegor\StringGenerator\CharRange;
use Taranovegor\StringGenerator\StringGenerator;

$generator = new StringGenerator();

$token    = $generator->generate(32);                     // a-zA-Z
$pin      = $generator->generate(6, CharRange::Numeric);  // 0-9
$password = $generator->generate(20, CharRange::Any);     // all printable ASCII
$hex      = $generator->generate(32, '0123456789abcdef'); // custom alphabet

Benchmark with PHP version 8.5.3, xdebug ❌, opcache ✔

\TaranovEgor\StringGenerator\Tests\Benchmark\StringGeneratorBench

Test Iterations Median Time Variation
benchNumeric10 I5 Mo3.431μs (±7.12%)
benchAlpha32 I5 Mo3.815μs (±3.21%)
benchAlphaNumeric64 I5 Mo4.770μs (±1.13%)
benchAny128 I5 Mo7.391μs (±7.40%)
benchSpecial32 I5 Mo3.182μs (±1.86%)
benchCustomHex32 I5 Mo2.905μs (±1.49%)
benchCustomBinary64 I5 Mo2.959μs (±2.24%)
benchCustom3CharRejection64 I5 Mo3.838μs (±4.66%)
benchAlpha1024 I5 Mo35.776μs (±0.44%)
benchAlpha8192 I5 Mo253.797μs (±4.11%)
benchNaiveAlphaNumeric64 I5 Mo50.231μs (±0.31%)

Subjects: 11, Assertions: 0, Failures: 0, Errors: 0

iter benchmark subject set revs mem_peak time_avg comp_z_value comp_deviation
0 StringGeneratorBench benchNumeric10 1000 1,930,080b 3.370μs -0.14σ -0.99%
1 StringGeneratorBench benchNumeric10 1000 1,930,080b 3.442μs +0.16σ +1.13%
2 StringGeneratorBench benchNumeric10 1000 1,930,080b 3.440μs +0.15σ +1.07%
3 StringGeneratorBench benchNumeric10 1000 1,930,080b 3.003μs -1.65σ -11.77%
4 StringGeneratorBench benchNumeric10 1000 1,930,080b 3.763μs +1.48σ +10.56%
0 StringGeneratorBench benchAlpha32 1000 1,930,080b 3.982μs +1.48σ +4.75%
1 StringGeneratorBench benchAlpha32 1000 1,930,080b 3.836μs +0.28σ +0.90%
2 StringGeneratorBench benchAlpha32 1000 1,930,080b 3.602μs -1.64σ -5.25%
3 StringGeneratorBench benchAlpha32 1000 1,930,080b 3.811μs +0.08σ +0.25%
4 StringGeneratorBench benchAlpha32 1000 1,930,080b 3.777μs -0.20σ -0.65%
0 StringGeneratorBench benchAlphaNumeric64 1000 1,930,128b 4.812μs +0.48σ +0.55%
1 StringGeneratorBench benchAlphaNumeric64 1000 1,930,128b 4.873μs +1.61σ +1.82%
2 StringGeneratorBench benchAlphaNumeric64 1000 1,930,128b 4.751μs -0.64σ -0.73%
3 StringGeneratorBench benchAlphaNumeric64 1000 1,487,296b 4.714μs -1.32σ -1.50%
4 StringGeneratorBench benchAlphaNumeric64 1000 1,487,296b 4.779μs -0.13σ -0.14%
0 StringGeneratorBench benchAny128 1000 1,487,248b 7.673μs +0.16σ +1.21%
1 StringGeneratorBench benchAny128 1000 1,487,248b 7.002μs -1.03σ -7.64%
2 StringGeneratorBench benchAny128 1000 1,487,248b 8.512μs +1.66σ +12.28%
3 StringGeneratorBench benchAny128 1000 1,487,248b 7.722μs +0.25σ +1.86%
4 StringGeneratorBench benchAny128 1000 1,487,248b 6.996μs -1.04σ -7.72%
0 StringGeneratorBench benchSpecial32 1000 1,487,248b 3.213μs +0.02σ +0.04%
1 StringGeneratorBench benchSpecial32 1000 1,487,248b 3.173μs -0.65σ -1.20%
2 StringGeneratorBench benchSpecial32 1000 1,487,248b 3.175μs -0.61σ -1.14%
3 StringGeneratorBench benchSpecial32 1000 1,487,248b 3.327μs +1.93σ +3.59%
4 StringGeneratorBench benchSpecial32 1000 1,487,248b 3.170μs -0.70σ -1.30%
0 StringGeneratorBench benchCustomHex32 1000 1,487,296b 2.925μs +1.15σ +1.71%
1 StringGeneratorBench benchCustomHex32 1000 1,487,296b 2.898μs +0.52σ +0.77%
2 StringGeneratorBench benchCustomHex32 1000 1,487,296b 2.830μs -1.07σ -1.59%
3 StringGeneratorBench benchCustomHex32 1000 1,487,296b 2.819μs -1.32σ -1.98%
4 StringGeneratorBench benchCustomHex32 1000 1,487,296b 2.907μs +0.73σ +1.08%
0 StringGeneratorBench benchCustomBinary64 1000 1,487,296b 3.082μs +1.18σ +2.65%
1 StringGeneratorBench benchCustomBinary64 1000 1,487,296b 2.988μs -0.21σ -0.48%
2 StringGeneratorBench benchCustomBinary64 1000 1,487,296b 2.942μs -0.90σ -2.01%
3 StringGeneratorBench benchCustomBinary64 1000 1,487,296b 2.921μs -1.21σ -2.71%
4 StringGeneratorBench benchCustomBinary64 1000 1,487,296b 3.079μs +1.14σ +2.55%
0 StringGeneratorBench benchCustom3CharRejection64 1000 1,487,344b 3.849μs -0.51σ -2.40%
1 StringGeneratorBench benchCustom3CharRejection64 1000 1,487,344b 3.816μs -0.69σ -3.24%
2 StringGeneratorBench benchCustom3CharRejection64 1000 1,487,344b 4.050μs +0.58σ +2.70%
3 StringGeneratorBench benchCustom3CharRejection64 1000 1,487,344b 3.751μs -1.05σ -4.88%
4 StringGeneratorBench benchCustom3CharRejection64 1000 1,487,344b 4.252μs +1.68σ +7.82%
0 StringGeneratorBench benchAlpha1024 500 1,487,248b 35.642μs -1.25σ -0.55%
1 StringGeneratorBench benchAlpha1024 500 1,487,248b 35.762μs -0.49σ -0.22%
2 StringGeneratorBench benchAlpha1024 500 1,487,248b 35.778μs -0.39σ -0.17%
3 StringGeneratorBench benchAlpha1024 500 1,487,248b 36.108μs +1.68σ +0.75%
4 StringGeneratorBench benchAlpha1024 500 1,487,248b 35.912μs +0.45σ +0.20%
0 StringGeneratorBench benchAlpha8192 100 1,487,248b 260.970μs +1.28σ +5.28%
1 StringGeneratorBench benchAlpha8192 100 1,487,248b 252.490μs +0.45σ +1.86%
2 StringGeneratorBench benchAlpha8192 100 1,487,248b 238.230μs -0.95σ -3.89%
3 StringGeneratorBench benchAlpha8192 100 1,487,248b 233.820μs -1.38σ -5.67%
4 StringGeneratorBench benchAlpha8192 100 1,487,248b 253.880μs +0.59σ +2.42%
0 StringGeneratorBench benchNaiveAlphaNumeric64 1000 1,487,344b 50.311μs +1.13σ +0.35%
1 StringGeneratorBench benchNaiveAlphaNumeric64 1000 1,487,344b 50.240μs +0.68σ +0.21%
2 StringGeneratorBench benchNaiveAlphaNumeric64 1000 1,487,344b 50.011μs -0.78σ -0.24%
3 StringGeneratorBench benchNaiveAlphaNumeric64 1000 1,487,344b 49.889μs -1.55σ -0.49%
4 StringGeneratorBench benchNaiveAlphaNumeric64 1000 1,487,344b 50.216μs +0.52σ +0.16%

Install

composer require taranovegor/string-generator

Full Changelog: https://github.com/taranovegor/string-generator/commits/v1.0.0