Skip to content

TOTP

  • Full name: \OTPHP\TOTP
  • Parent class: \OTPHP\OTP
  • This class is marked as final and can't be subclassed
  • This class implements: \OTPHP\TOTPInterface
  • This class is a Final class

See Also:

  • \OTPHP\Test\TOTPTest -

Methods

create

Create a new TOTP object.

public static create(null|string $secret = null, int $period = self::DEFAULT_PERIOD, string $digest = self::DEFAULT_DIGEST, int $digits = self::DEFAULT_DIGITS, int $epoch = self::DEFAULT_EPOCH): self

If the secret is null, a random 64 bytes secret will be generated.

  • This method is static.

Parameters:

Parameter Type Description
$secret null|string
$period int
$digest string
$digits int
$epoch int

createFromSecret

Create a OTP object from an existing secret.

public static createFromSecret(string $secret): self
  • This method is static.

Parameters:

Parameter Type Description
$secret string

generate

Create a new OTP object. A random 64 bytes secret will be generated.

public static generate(): self
  • This method is static.

getPeriod

Get the period of time for OTP generation (a non-null positive integer, in second).

public getPeriod(): positive-int

getEpoch

public getEpoch(): 0|positive-int

expiresIn

public expiresIn(): 0|positive-int

at

public at(int $input): non-empty-string

Parameters:

Parameter Type Description
$input int

Return Value:

Return the OTP at the specified timestamp


now

Return the TOTP at the current time.

public now(): non-empty-string

verify

If no timestamp is provided, the OTP is verified at the actual timestamp. When used, the leeway parameter will allow time drift. The passed value is in seconds.

public verify(string $otp, null|int $timestamp = null, null|int $leeway = null): bool

Parameters:

Parameter Type Description
$otp string
$timestamp null|int
$leeway null|int

getProvisioningUri

Get the provisioning URI.

public getProvisioningUri(): non-empty-string

setPeriod

public setPeriod(int $period): void

Parameters:

Parameter Type Description
$period int

setEpoch

public setEpoch(int $epoch): void

Parameters:

Parameter Type Description
$epoch int

getParameterMap

protected getParameterMap(): array<non-empty-string,callable>

filterOptions

protected filterOptions(array<non-empty-string,mixed>& $options): void

Parameters:

Parameter Type Description
$options array

timecode

private timecode(0|positive-int $timestamp): 0|positive-int

Parameters:

Parameter Type Description
$timestamp 0|positive-int

Inherited methods

getParameters

public getParameters(): array<non-empty-string,mixed>

getSecret

public getSecret(): string

getLabel

public getLabel(): null|string

setLabel

public setLabel(string $label): void

Parameters:

Parameter Type Description
$label string

getIssuer

public getIssuer(): null|string

setIssuer

public setIssuer(string $issuer): void

Parameters:

Parameter Type Description
$issuer string

isIssuerIncludedAsParameter

public isIssuerIncludedAsParameter(): bool

setIssuerIncludedAsParameter

public setIssuerIncludedAsParameter(bool $issuer_included_as_parameter): void

Parameters:

Parameter Type Description
$issuer_included_as_parameter bool

getDigits

public getDigits(): int

getDigest

public getDigest(): string

hasParameter

public hasParameter(string $parameter): bool

Parameters:

Parameter Type Description
$parameter string

getParameter

public getParameter(string $parameter): mixed

Parameters:

Parameter Type Description
$parameter string

setParameter

public setParameter(string $parameter, mixed $value): void

Parameters:

Parameter Type Description
$parameter string
$value mixed

setSecret

public setSecret(string $secret): void

Parameters:

Parameter Type Description
$secret string

setDigits

public setDigits(int $digits): void

Parameters:

Parameter Type Description
$digits int

setDigest

public setDigest(string $digest): void

Parameters:

Parameter Type Description
$digest string

getParameterMap

protected getParameterMap(): array<non-empty-string,callable>

hasColon

private hasColon(non-empty-string $value): bool

Parameters:

Parameter Type Description
$value non-empty-string

__construct

protected __construct(non-empty-string $secret): mixed

Parameters:

Parameter Type Description
$secret non-empty-string

getQrCodeUri

Get the provisioning URI.

public getQrCodeUri(string $uri, string $placeholder): string

Parameters:

Parameter Type Description
$uri string The Uri of the QRCode generator with all parameters. This Uri MUST contain a placeholder that will be replaced by the method.
$placeholder string the placeholder to be replaced in the QR Code generator URI

at

public at(int $input): non-empty-string

Parameters:

Parameter Type Description
$input int

Return Value:

Return the OTP at the specified timestamp


generateSecret

final protected static generateSecret(): non-empty-string
  • This method is static.

  • This method is final.


generateOTP

The OTP at the specified input.

protected generateOTP(0|positive-int $input): non-empty-string

Parameters:

Parameter Type Description
$input 0|positive-int

filterOptions

protected filterOptions(array<non-empty-string,mixed>& $options): void

Parameters:

Parameter Type Description
$options array

generateURI

protected generateURI(non-empty-string $type, array<non-empty-string,mixed> $options): non-empty-string

Parameters:

Parameter Type Description
$type non-empty-string
$options array

compareOTP

protected compareOTP(non-empty-string $safe, non-empty-string $user): bool

Parameters:

Parameter Type Description
$safe non-empty-string
$user non-empty-string

getDecodedSecret

private getDecodedSecret(): non-empty-string

intToByteString

private intToByteString(0|positive-int $int): string

Parameters:

Parameter Type Description
$int 0|positive-int


Automatically generated on 2025-03-18