public getCode ( $secret, $time = null ) |
public function getCode($secret, $time = null)
{
$secretkey = $this->base32Decode($secret);
$timestamp = "" . pack('N*', $this->getTimeSlice($this->getTime($time)));
// Pack time into binary string
$hashhmac = hash_hmac($this->algorithm, $timestamp, $secretkey, true);
// Hash it with users secret key
$hashpart = substr($hashhmac, ord(substr($hashhmac, -1)) & 0xf, 4);
// Use last nibble of result as index/offset and grab 4 bytes of the result
$value = unpack('N', $hashpart);
// Unpack binary value
$value = $value[1] & 0x7fffffff;
// Drop MSB, keep only 31 bits
return str_pad($value % pow(10, $this->digits), $this->digits, '0', STR_PAD_LEFT);
}
public function testKnownTestVectors_sha512() { //Known test vectors for SHA512: https://tools.ietf.org/html/rfc6238#page-15 $secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA'; //== base32encode('1234567890123456789012345678901234567890123456789012345678901234') $tfa = new TwoFactorAuth('Test', 8, 30, 'sha512'); $this->assertEquals('90693936', $tfa->getCode($secret, 59)); $this->assertEquals('25091201', $tfa->getCode($secret, 1111111109)); $this->assertEquals('99943326', $tfa->getCode($secret, 1111111111)); $this->assertEquals('93441116', $tfa->getCode($secret, 1234567890)); $this->assertEquals('38618901', $tfa->getCode($secret, 2000000000)); $this->assertEquals('47863826', $tfa->getCode($secret, 20000000000)); }