A small PHP library for generating one-time passwords and random verification codes.

Maintainers

Package info

github.com/elephant-php/otp

pkg:composer/elephant-php/otp

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.0 2026-05-01 09:43 UTC

This package is not auto-updated.

Last update: 2026-05-02 07:57:29 UTC


README

elephant-php

Elephant OTP

A small PHP library for generating one-time passwords and random verification codes.

Latest Version License PHP 8.3+


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.