Back to Totp class

Method getCode

public string
getCode
(mixed $secret, mixed $time = null)
Gets the TOTP passcode for a given secret key $secret and a given UNIX timestamp $time
Parameters
  • string $secret The Base32-encoded secret key
  • int $time UNIX timestamp
Returns
  • string
Class: Totp
Project: Joomla

Method getCode - Source code

/**
 * Gets the TOTP passcode for a given secret key $secret and a given UNIX
 * timestamp $time
 *
 * @param   string  $secret  The Base32-encoded secret key
 * @param   int     $time    UNIX timestamp
 *
 * @return string
 */
public function getCode($secret, $time = null)
{
    $period = $this->getPeriod($time);
    $secret = $this->_base32->decode($secret);
    $time = pack("N", $period);
    $time = str_pad($time, 8, \chr(0), STR_PAD_LEFT);
    $hash = hash_hmac('sha1', $time, $secret, true);
    $offset = \ord(substr($hash, -1));
    $offset = $offset & 0xf;
    $truncatedHash = $this->hashToInt($hash, $offset) & 0x7fffffff;
    $pinValue = str_pad($truncatedHash % $this->_pinModulo, $this->_passCodeLength, "0", STR_PAD_LEFT);
    return $pinValue;
}