FOF30\Encrypt\Totp::getCode PHP Метод

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
    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;
    }

Usage Example

Пример #1
0
 public function testCheckCode()
 {
     $secret = '4FDAGLLSP6BIVU5H';
     $time = 1375000339;
     $code = $this->totp->getCode($secret, $time);
     $codePrev = $this->totp->getCode($secret, $time - 30);
     $codeNext = $this->totp->getCode($secret, $time + 30);
     $this->assertTrue($this->totp->checkCode($secret, $code, $time));
     $this->assertTrue($this->totp->checkCode($secret, $codePrev, $time));
     $this->assertTrue($this->totp->checkCode($secret, $codeNext, $time));
 }
All Usage Examples Of FOF30\Encrypt\Totp::getCode