phlak/strgen
Generate secure random strings (e.g. passwords / salts)
Fund package maintenance!
PHLAK
Patreon
paypal.me/ChrisKankiewicz
Requires
- php: >=5.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.3
- phpunit/phpunit: ^5.7
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT 652ff7395f6a515a8b9baf1ac60bc6abf921477f
- Chris Kankiewicz<Chris.woop@ChrisKankiewicz.com>
- dev-master
- 3.0.0
- 2.0.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- dev-dependabot/composer/yoast/phpunit-polyfills-tw-2.0
- dev-dependabot/add-v2-config-file
This package is auto-updated.
Last update: 2025-03-01 00:12:55 UTC
README
PHP library for simple secure random string generation (e.g. - passwords / salts)
Created byChris Kankiewicz (@PHLAK)
Requirements
- PHP >= 7.2
Install with Composer
composer require phlak/strgen
Usage
// Import StrGenusePHLAK\StrGen;// Initialize the Generator$generator =newStrGen\Generator();// Generate a random string of characters$generator->length(16)->generate();// Returns something like '8a*Ag@I0*s0v[S3u'
Character Sets
StrGen has a few built-in character sets available for ease of use. You canspecify which set(s) to use by passing a character set or an array of sets tothecharset()
method.
Example using built-in sets:
$generator =newStrGen\Generator();$generator->charset(StrGen\CharSet::ALPHA_NUMERIC)->generate();// or$generator->charset([StrGen\CharSet::MIXED_ALPHA,StrGen\CharSet::NUMERIC])->generate();
Available presets:
Key | Character Set |
---|---|
StrGen\CharSet::LOWER_ALPHA | abcdefghijklmnopqrstuvwxyz |
StrGen\CharSet::UPPER_ALPHA | ABCDEFGHIJKLMNOPQRSTUVWXYZ |
StrGen\CharSet::MIXED_ALPHA | abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ |
StrGen\CharSet::NUMERIC | 0123456789 |
StrGen\CharSet::ALPHA_NUMERIC | abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 |
StrGen\CharSet::SPECIAL | !@#$%^&*()-_=+.?{}[]<>:;/\|~ |
StrGen\CharSet::ALL | abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 !@#$%^&*()-_=+.?{}[]<>:;/\|~ |
Custom sets:
You can also manually define a character set by passing a string of charactersto thecharset()
method.
$generator =newStrGen\Generator();$generator->charset('0123456789abcdef')->generate();
Convenience Functions
StrGen also has built-in convenience functions for generating strings from theincluded character sets or a custom character set.
$generator->lowerAlpha($length);$generator->upperAlpha($length);$generator->mixedAlpha($length);$generator->numeric($length);$generator->alphaNumeric($length);$generator->special($length);$generator->all($length);$generator->custom($length,$charset);
Changelog
A list of changes can be found on theGitHub Releases page.
Troubleshooting
For general help and support join ourSpectrum community.
Please report bugs to theGitHub Issue Tracker.
Copyright
This project is licensed under theMIT License.