document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. /**
  3.  * This program is free software: you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation, either version 3 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  *
  16.  * PHP Google two-factor authentication module.
  17.  *
  18.  * See http://www.idontplaydarts.com/2011/07/google-totp-two-factor-authentication-for-php/
  19.  * for more details
  20.  *
  21.  * @author Phil
  22.  **/
  23.  
  24. class Google2FA {
  25.  
  26.     const keyRegeneration   = 30;   // Interval between key regeneration
  27.     const otpLength     = 6;    // Length of the Token generated
  28.  
  29.     private static $lut = array(    // Lookup needed for Base32 encoding
  30.         "A" => 0,   "B" => 1,
  31.         "C" => 2,   "D" => 3,
  32.         "E" => 4,   "F" => 5,
  33.         "G" => 6,   "H" => 7,
  34.         "I" => 8,   "J" => 9,
  35.         "K" => 10,  "L" => 11,
  36.         "M" => 12,  "N" => 13,
  37.         "O" => 14,  "P" => 15,
  38.         "Q" => 16,  "R" => 17,
  39.         "S" => 18,  "T" => 19,
  40.         "U" => 20,  "V" => 21,
  41.         "W" => 22,  "X" => 23,
  42.         "Y" => 24,  "Z" => 25,
  43.         "2" => 26,  "3" => 27,
  44.         "4" => 28,  "5" => 29,
  45.         "6" => 30,  "7" => 31
  46.     );
  47.  
  48.     /**
  49.      * Generates a 16 digit secret key in base32 format
  50.      * @return string
  51.      **/
  52.     public static function generate_secret_key($length = 16) {
  53.         $b32    = "234567QWERTYUIOPASDFGHJKLZXCVBNM";
  54.         $s  = "";
  55.  
  56.         for ($i = 0; $i < $length; $i++)
  57.             $s .= $b32[rand(0,31)];
  58.  
  59.         return $s;
  60.     }
  61.  
  62.     /**
  63.      * Returns the current Unix Timestamp devided by the keyRegeneration
  64.      * period.
  65.      * @return integer
  66.      **/
  67.     public static function get_timestamp() {
  68.         return floor(microtime(true)/self::keyRegeneration);
  69.     }
  70.  
  71.     /**
  72.      * Decodes a base32 string into a binary string.
  73.      **/
  74.     public static function base32_decode($b32) {
  75.  
  76.         $b32    = strtoupper($b32);
  77.  
  78.         if (!preg_match(\'/^[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]+$/\', $b32, $match))
  79.             throw new Exception(\'Invalid characters in the base32 string.\');
  80.  
  81.         $l  = strlen($b32);
  82.         $n  = 0;
  83.         $j  = 0;
  84.         $binary = "";
  85.  
  86.         for ($i = 0; $i < $l; $i++) {
  87.  
  88.             $n = $n << 5;               // Move buffer left by 5 to make room
  89.             $n = $n + self::$lut[$b32[$i]];     // Add value into buffer
  90.             $j = $j + 5;                // Keep track of number of bits in buffer
  91.  
  92.             if ($j >= 8) {
  93.                 $j = $j - 8;
  94.                 $binary .= chr(($n & (0xFF << $j)) >> $j);
  95.             }
  96.         }
  97.  
  98.         return $binary;
  99.     }
  100.  
  101.     /**
  102.      * Takes the secret key and the timestamp and returns the one time
  103.      * password.
  104.      *
  105.      * @param binary $key - Secret key in binary form.
  106.      * @param integer $counter - Timestamp as returned by get_timestamp.
  107.      * @return string
  108.      **/
  109.     public static function oath_hotp($key, $counter)
  110.     {
  111.         if (strlen($key) < 8)
  112.         throw new Exception(\'Secret key is too short. Must be at least 16 base 32 characters\');
  113.  
  114.         $bin_counter = pack(\'N*\', 0) . pack(\'N*\', $counter);        // Counter must be 64-bit int
  115.         $hash    = hash_hmac (\'sha1\', $bin_counter, $key, true);
  116.  
  117.         return str_pad(self::oath_truncate($hash), self::otpLength, \'0\', STR_PAD_LEFT);
  118.     }
  119.  
  120.     /**
  121.      * Verifys a user inputted key against the current timestamp. Checks $window
  122.      * keys either side of the timestamp.
  123.      *
  124.      * @param string $b32seed
  125.      * @param string $key - User specified key
  126.      * @param integer $window
  127.      * @param boolean $useTimeStamp
  128.      * @return boolean
  129.      **/
  130.     public static function verify_key($b32seed, $key, $window = 4, $useTimeStamp = true) {
  131.  
  132.         $timeStamp = self::get_timestamp();
  133.  
  134.         if ($useTimeStamp !== true) $timeStamp = (int)$useTimeStamp;
  135.  
  136.         $binarySeed = self::base32_decode($b32seed);
  137.  
  138.         for ($ts = $timeStamp - $window; $ts <= $timeStamp + $window; $ts++)
  139.             if (self::oath_hotp($binarySeed, $ts) == $key)
  140.                 return true;
  141.  
  142.         return false;
  143.  
  144.     }
  145.  
  146.     /**
  147.      * Extracts the OTP from the SHA1 hash.
  148.      * @param binary $hash
  149.      * @return integer
  150.      **/
  151.     public static function oath_truncate($hash)
  152.     {
  153.         $offset = ord($hash[19]) & 0xf;
  154.  
  155.         return (
  156.             ((ord($hash[$offset+0]) & 0x7f) << 24 ) |
  157.             ((ord($hash[$offset+1]) & 0xff) << 16 ) |
  158.             ((ord($hash[$offset+2]) & 0xff) << 8 ) |
  159.             (ord($hash[$offset+3]) & 0xff)
  160.         ) % pow(10, self::otpLength);
  161.     }
  162. }
  163.  
  164. $InitalizationKey = "AAABBBCCCDDDEEEF";                         // Set the inital key
  165.  
  166. $TimeStamp    = Google2FA::get_timestamp();
  167. $secretkey    = Google2FA::base32_decode($InitalizationKey);    // Decode it into binary
  168. $otp          = Google2FA::oath_hotp($secretkey, $TimeStamp);   // Get current token
  169.  
  170. echo("Init key: $InitalizationKey\\n");
  171. echo("Timestamp: $TimeStamp\\n");
  172. echo("One time password: $otp\\n");
  173.  
  174. // Use this to verify a key as it allows for some time drift.
  175.  
  176. $result = Google2FA::verify_key($InitalizationKey, "123456");
  177.  
  178. var_dump($result);
');