Skip to content

elephant-php/otp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.