diff --git a/lib/hotp.php b/lib/hotp.php index 7092fd9..3eff087 100644 --- a/lib/hotp.php +++ b/lib/hotp.php @@ -41,8 +41,8 @@ class HOTP extends OTP { * seed the hmac hash function. * @return integer the One Time Password */ - public function at($count) { - return $this->generateOTP($count); + public function at($count,$g2f=false) { + return $this->generateOTP($count,$g2f); } diff --git a/lib/otp.php b/lib/otp.php index 77bcfe9..2a3acb2 100644 --- a/lib/otp.php +++ b/lib/otp.php @@ -78,7 +78,7 @@ public function __construct($secret, $opt = Array()) { * timestamp (see TOTP class). * @return integer the one-time password */ - public function generateOTP($input) { + public function generateOTP($input,$g2f = false) { $hash = hash_hmac($this->digest, $this->intToBytestring($input), $this->byteSecret()); foreach(str_split($hash, 2) as $hex) { // stupid PHP has bin2hex but no hex2bin WTF $hmac[] = hexdec($hex); @@ -88,8 +88,15 @@ public function generateOTP($input) { ($hmac[$offset + 1] & 0xFF) << 16 | ($hmac[$offset + 2] & 0xFF) << 8 | ($hmac[$offset + 3] & 0xFF); + if($g2f) return $this->normalizeOTP($code % pow(10, $this->digits)); return $code % pow(10, $this->digits); } + /* + Add initial zeros for match with Google Authenticator App + */ + public function normalizeOTP($otp){ + return str_pad($otp,6,"0",STR_PAD_LEFT); + } /** * Returns the binary value of the base32 encoded secret diff --git a/lib/totp.php b/lib/totp.php index 10a1f42..845af8f 100644 --- a/lib/totp.php +++ b/lib/totp.php @@ -54,8 +54,8 @@ public function __construct($s, $opt = Array()) { * used to seed the hmac hash function. * @return integer the One Time Password */ - public function at($timestamp) { - return $this->generateOTP($this->timecode($timestamp)); + public function at($timestamp,$g2f = false) { + return $this->generateOTP($this->timecode($timestamp),$g2f); } /** @@ -63,8 +63,8 @@ public function at($timestamp) { * * @return integer the current One Time Password */ - public function now() { - return $this->generateOTP($this->timecode(time())); + public function now($g2f = false) { + return $this->generateOTP($this->timecode(time()),$g2f); } /**