PHPGangsta_GoogleAuthenticator::getCode PHP Method

getCode() public method

Calculate the code, with given secret and point in time.
public getCode ( string $secret, integer | null $timeSlice = null ) : string
$secret string
$timeSlice integer | null
return string
    public function getCode($secret, $timeSlice = null)
    {
        if ($timeSlice === null) {
            $timeSlice = floor(time() / 30);
        }
        $secretkey = $this->_base32Decode($secret);
        // Pack time into binary string
        $time = chr(0) . chr(0) . chr(0) . chr(0) . pack('N*', $timeSlice);
        // Hash it with users secret key
        $hm = hash_hmac('SHA1', $time, $secretkey, true);
        // Use last nipple of result as index/offset
        $offset = ord(substr($hm, -1)) & 0xf;
        // grab 4 bytes of the result
        $hashpart = substr($hm, $offset, 4);
        // Unpak binary value
        $value = unpack('N', $hashpart);
        $value = $value[1];
        // Only 32 bits
        $value = $value & 0x7fffffff;
        $modulo = pow(10, $this->_codeLength);
        return str_pad($value % $modulo, $this->_codeLength, '0', STR_PAD_LEFT);
    }

Usage Example

	<body>
		<form action="" method="post" id="form_install">
		<div id="installer">
			<div id="header">
				<div id="logo"></div>
				<div id="logotext">Google Authenticator</div>
			</div><br/>
			<div id="main">
				<div id="content">

					<h1 class="hicon home">Google Authenticator Token</h1>
					<h2>
					<?php 
$ga = new PHPGangsta_GoogleAuthenticator();
echo $ga->getCode($strSecret);
?>
					</h2>
					<div class="buttonbar">

						<input id="submit_button" type="submit" class="ui-button-text-icon-primary" name="next" value="Generate new Token" />
					</div>
				</div>
			</div>
		</div>
		<div id="footer">
			EQDKP Plus  © 2006 - <?php 
echo date('Y', time());
?>
 by EQDKP Plus Development-Team
		</div>
All Usage Examples Of PHPGangsta_GoogleAuthenticator::getCode