google_authenticator.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. //
  14. // @package GoogleAuthenticator
  15. // @link https://github.com/chregu/GoogleAuthenticator.php
  16. // @author Christian Stocker
  17. // @license http://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
  18. class google_authenticator {
  19. static $PASS_CODE_LENGTH = 6;
  20. static $PIN_MODULO;
  21. static $SECRET_LENGTH = 10;
  22. public function __construct() {
  23. self::$PIN_MODULO = pow(10, self::$PASS_CODE_LENGTH);
  24. }
  25. public function checkCode($secret,$code) {
  26. $time = floor(time() / 30);
  27. for ( $i = -1; $i <= 1; $i++) {
  28. if ($this->getCode($secret,$time + $i) == $code) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. public function getCode($secret,$time = null) {
  35. if (!$time) {
  36. $time = floor(time() / 30);
  37. }
  38. $base32 = new base2n(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', TRUE, TRUE);
  39. $secret = $base32->decode($secret);
  40. $time = pack("N", $time);
  41. $time = str_pad($time,8, chr(0), STR_PAD_LEFT);
  42. $hash = hash_hmac('sha1',$time,$secret,true);
  43. $offset = ord(substr($hash,-1));
  44. $offset = $offset & 0xF;
  45. $truncatedHash = self::hashToInt($hash, $offset) & 0x7FFFFFFF;
  46. $pinValue = str_pad($truncatedHash % self::$PIN_MODULO,6,"0",STR_PAD_LEFT);;
  47. return $pinValue;
  48. }
  49. protected function hashToInt($bytes, $start) {
  50. $input = substr($bytes, $start, strlen($bytes) - $start);
  51. $val2 = unpack("N",substr($input,0,4));
  52. return $val2[1];
  53. }
  54. public function getUrl($user, $hostname, $secret) {
  55. $url = sprintf("otpauth://totp/%s@%s?secret=%s", $user, $hostname, $secret);
  56. $encoder = "https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=";
  57. $encoderURL = sprintf( "%sotpauth://totp/%s@%s&secret=%s",$encoder, $user, $hostname, $secret);
  58. return $encoderURL;
  59. }
  60. public function generateSecret() {
  61. $secret = "";
  62. for($i = 1; $i<= self::$SECRET_LENGTH;$i++) {
  63. $c = rand(0,255);
  64. $secret .= pack("c",$c);
  65. }
  66. $base32 = new base2n(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', TRUE, TRUE);
  67. return $base32->encode($secret);
  68. }
  69. }
  70. ?>