Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/hotp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
9 changes: 8 additions & 1 deletion lib/otp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/totp.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ 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);
}

/**
* Get the password for the current timestamp value
*
* @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);
}

/**
Expand Down