Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GoogleAuthenticator.php #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 6 additions & 18 deletions lib/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.


include_once("FixedByteNotation.php");


class GoogleAuthenticator {
static $PASS_CODE_LENGTH = 6;
static $PIN_MODULO;
Expand All @@ -32,20 +30,17 @@ public function checkCode($secret,$code) {
return true;
}
}

return false;

}

public function getCode($secret,$time = null) {

if (!$time) {
$time = floor(time() / 30);
}
$base32 = new FixedBitNotation(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', TRUE, TRUE);
$secret = $base32->decode($secret);

$time = pack("N", $time);
$time = pack('N', $time);
$time = str_pad($time,8, chr(0), STR_PAD_LEFT);

$hash = hash_hmac('sha1',$time,$secret,true);
Expand All @@ -59,30 +54,23 @@ public function getCode($secret,$time = null) {

protected function hashToInt($bytes, $start) {
$input = substr($bytes, $start, strlen($bytes) - $start);
$val2 = unpack("N",substr($input,0,4));
$val2 = unpack('N',substr($input,0,4));
return $val2[1];
}

public function getUrl($user, $hostname, $secret) {
$url = sprintf("otpauth://totp/%s@%s?secret=%s", $user, $hostname, $secret);
$encoder = "https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=";
$encoderURL = sprintf( "%sotpauth://totp/%s@%s&secret=%s",$encoder, $user, $hostname, $secret);

$encoder = "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=";
$encoderURL = $encoder . urlencode("otpauth://totp/{$user}@{$hostname}?secret={$secret}&issuer={$name}");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the var $name?

return $encoderURL;

}

public function generateSecret() {
$secret = "";
for($i = 1; $i<= self::$SECRET_LENGTH;$i++) {
$secret = null;
for($i = 1;$i<= self::$SECRET_LENGTH;$i++) {
$c = rand(0,255);
$secret .= pack("c",$c);
}
$base32 = new FixedBitNotation(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', TRUE, TRUE);
return $base32->encode($secret);


}

}