Source for file totp.php
Documentation is available at totp.php
* @package FrameworkOnFramework
* @copyright Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* This class provides an RFC6238-compliant Time-based One Time Passwords,
* compatible with Google Authenticator (with PassCodeLength = 6 and TimePeriod = 30).
* @package FrameworkOnFramework
private $_passCodeLength =
6;
private $_secretLength =
10;
* 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.
* @param int $timeStep The Time Step (in seconds). Use 30 to be compatible with Google Authenticator.
* @param int $passCodeLength The generated passcode length. Default: 6 digits.
* @param int $secretLength The length of the secret key. Default: 10 bytes (80 bits).
public function __construct($timeStep =
30, $passCodeLength =
6, $secretLength =
10)
$this->_timeStep =
$timeStep;
$this->_passCodeLength =
$passCodeLength;
$this->_secretLength =
$secretLength;
$this->_pinModulo =
pow(10, $this->_passCodeLength);
* 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
* @param int|null $time Timestamp
* @return int The time period since the UNIX Epoch
$period =
floor($time /
$this->_timeStep);
* Check is the given passcode $code is a valid TOTP generated using secret
* @param string $secret The Base32-encoded secret key
* @param string $code The passcode to check
* @return boolean True if the code is valid
for ($i = -
1; $i <=
1; $i++
)
if ($this->getCode($secret, $time +
$i) ==
$code)
* Gets the TOTP passcode for a given secret key $secret and a given UNIX
* @param string $secret The Base32-encoded secret key
* @param int $time UNIX timestamp
public function getCode($secret, $time =
null)
$secret =
$base32->decode($secret);
$time =
pack("N", $period);
$hash =
hash_hmac('sha1', $time, $secret, true);
$truncatedHash =
$this->hashToInt($hash, $offset) & 0x7FFFFFFF;
$pinValue =
str_pad($truncatedHash %
$this->_pinModulo, $this->_passCodeLength, "0", STR_PAD_LEFT);
* Extracts a part of a hash as an integer
* @param string $bytes The hash
* @param string $start The char to start from (0 = first char)
* Returns a QR code URL for easy setup of TOTP apps like Google Authenticator
* @param string $user User
* @param string $hostname Hostname
* @param string $secret Secret string
public function getUrl($user, $hostname, $secret)
$url =
sprintf("otpauth://totp/%s@%s?secret=%s", $user, $hostname, $secret);
$encoder =
"https://chart.googleapis.com/chart?chs=200x200&chld=Q|2&cht=qr&chl=";
* Generates a (semi-)random Secret Key for TOTP generation
for ($i =
1; $i <=
$this->_secretLength; $i++
)
$secret .=
pack("c", $c);
return $base32->encode($secret);
Documentation generated on Tue, 19 Nov 2013 15:15:56 +0100 by phpDocumentor 1.4.3