elephant-php / otp
A small PHP library for generating one-time passwords and random verification codes.
0.1.0
2026-05-01 09:43 UTC
Requires
- php: ^8.3
Requires (Dev)
- testo/testo: ^0.10.2
This package is not auto-updated.
Last update: 2026-05-02 07:57:29 UTC
README
Elephant OTP
A small PHP library for generating one-time passwords and random verification codes.
Installation
composer require elephant-php/otp
Usage
Generate 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
Alphabets
| 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.
Invalid Length
Code length must be at least 1. Invalid lengths throw InvalidArgumentException:
use Elephant\Otp\OtpCode; new OtpCode()->generate(0); // throws InvalidArgumentException
Testing
composer test
License
MIT License.
Please see LICENSE for more information.