OTP Utilities

Lightpack’s Otp utility provides a fluent, flexible API for generating one-time passwords (OTPs) and codes for authentication, verification, or any scenario where you need a secure, random code.


Overview


Basic Usage

use Lightpack\Utils\Otp;

$otp = new Otp();
$code = $otp->generate(); // 6-digit numeric code by default

Fluent Configuration

Set Code Length

$otp->length(4); // 4 characters

Set Code Type

$otp->type('numeric'); // Default, e.g. 123456
$otp->type('alpha');   // e.g. ABCDEF
$otp->type('alnum');   // e.g. 1A2B3C
$otp->type('custom')->charset('XYZ123'); // e.g. 1Z2XY

Full Example

$otp = (new Otp)
    ->length(8)
    ->type('alnum');

$code = $otp->generate(); // e.g. 1A2B3C4D

Custom Charset

You can fully control the character set:

$otp = (new Otp)
    ->length(5)
    ->type('custom')
    ->charset('ABC123');

$code = $otp->generate(); // e.g. 1B2CA

Edge Cases & Defaults