A small PHP library for generating one-time passwords and random verification codes.
composer require elephant-php/otpGenerate a default 6-character code:
use Elephant\Otp\OtpCode;
$code = new OtpCode()->generate();Generate a code with a custom length:
use Elephant\Otp\OtpCode;
$code = new OtpCode()->generate(12);Choose an alphabet:
use Elephant\Otp\Alphabet;
use Elephant\Otp\OtpCode;
$otp = new OtpCode();
$numeric = $otp->generate(alphabet: Alphabet::Numeric); // 483920
$hex = $otp->generate(16, Alphabet::Hex); // 9f03a8c12b4e70df
$readable = $otp->generate(10, Alphabet::Readable); // 7h4qz9x2wa| Alphabet | Characters |
|---|---|
Alphabet::Numeric |
0123456789 |
Alphabet::Hex |
0123456789abcdef |
Alphabet::Alpha |
abcdefghijklmnopqrstuvwxyz |
Alphabet::AlphaNumeric |
abcdefghijklmnopqrstuvwxyz0123456789 |
Alphabet::Readable |
23456789abcdefghijkmnpqrstuvwxyz |
Alphabet::Readable excludes visually ambiguous characters such as 0, 1, l, and o.
Code length must be at least 1. Invalid lengths throw InvalidArgumentException:
use Elephant\Otp\OtpCode;
new OtpCode()->generate(0); // throws InvalidArgumentExceptioncomposer testMIT License.
Please see LICENSE for more information.