PHP Класс FOF30\Encrypt\Totp

Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__construct ( integer $timeStep = 30, integer $passCodeLength = 6, integer $secretLength = 10, Base32 $base32 = null ) Initialises an RFC6238-compatible TOTP generator. Please note that this class does not implement the constraint in the last paragraph of §5.2 of RFC6238. It's up to you to ensure that the same user/device does not retry validation within the same Time Step.
checkCode ( string $secret, string $code, integer $time = null ) : boolean Check is the given passcode $code is a valid TOTP generated using secret key $secret
generateSecret ( ) : string Generates a (semi-)random Secret Key for TOTP generation
getCode ( string $secret, integer $time = null ) : string Gets the TOTP passcode for a given secret key $secret and a given UNIX timestamp $time
getPeriod ( integer | null $time = null ) : integer Get the time period based on the $time timestamp and the Time Step defined. If $time is skipped or set to null the current timestamp will be used.
getUrl ( string $user, string $hostname, string $secret ) : string Returns a QR code URL for easy setup of TOTP apps like Google Authenticator

Защищенные методы

Метод Описание
hashToInt ( string $bytes, string $start ) : string Extracts a part of a hash as an integer

Описание методов

__construct() публичный Метод

Initialises an RFC6238-compatible TOTP generator. Please note that this class does not implement the constraint in the last paragraph of §5.2 of RFC6238. It's up to you to ensure that the same user/device does not retry validation within the same Time Step.
public __construct ( integer $timeStep = 30, integer $passCodeLength = 6, integer $secretLength = 10, Base32 $base32 = null )
$timeStep integer The Time Step (in seconds). Use 30 to be compatible with Google Authenticator.
$passCodeLength integer The generated passcode length. Default: 6 digits.
$secretLength integer The length of the secret key. Default: 10 bytes (80 bits).
$base32 Base32 The base32 en/decrypter

checkCode() публичный Метод

Check is the given passcode $code is a valid TOTP generated using secret key $secret
public checkCode ( string $secret, string $code, integer $time = null ) : boolean
$secret string The Base32-encoded secret key
$code string The passcode to check
$time integer The time to check it against. Leave null to check for the current server time.
Результат boolean True if the code is valid

generateSecret() публичный Метод

Generates a (semi-)random Secret Key for TOTP generation
public generateSecret ( ) : string
Результат string

getCode() публичный Метод

Gets the TOTP passcode for a given secret key $secret and a given UNIX timestamp $time
public getCode ( string $secret, integer $time = null ) : string
$secret string The Base32-encoded secret key
$time integer UNIX timestamp
Результат string

getPeriod() публичный Метод

Get the time period based on the $time timestamp and the Time Step defined. If $time is skipped or set to null the current timestamp will be used.
public getPeriod ( integer | null $time = null ) : integer
$time integer | null Timestamp
Результат integer The time period since the UNIX Epoch

getUrl() публичный Метод

Returns a QR code URL for easy setup of TOTP apps like Google Authenticator
public getUrl ( string $user, string $hostname, string $secret ) : string
$user string User
$hostname string Hostname
$secret string Secret string
Результат string

hashToInt() защищенный Метод

Extracts a part of a hash as an integer
protected hashToInt ( string $bytes, string $start ) : string
$bytes string The hash
$start string The char to start from (0 = first char)
Результат string