Repository: lelag/otphp Branch: master Commit: bbe634327b04 Files: 13 Total size: 27.0 KB Directory structure: gitextract_ufs2v9hq/ ├── .gitignore ├── LICENCE ├── README.markdown ├── lib/ │ ├── hotp.php │ ├── otp.php │ ├── otphp.php │ └── totp.php ├── tests/ │ ├── HOTPTest.php │ ├── OTPTest.php │ ├── TOTPTest.php │ └── TestTest.php └── vendor/ ├── base32.php └── libs.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .bak ================================================ FILE: LICENCE ================================================ Copyright (c) 2011 Le Lag Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.markdown ================================================ # OTPHP - A PHP One Time Password Library A php library for generating one time passwords according to [ RFC 4226 ](http://tools.ietf.org/html/rfc4226) and the [ HOTP RFC ](http://tools.ietf.org/html/draft-mraihi-totp-timebased-00) This is compatible with Google Authenticator apps available for Android and iPhone, and now in use on GMail This is a port of the rotp ruby library available at https://github.com/mdp/rotp Note : This project has not been updated since it was created. If you need a PHP OTP library, you should check out the great fork by Spomky-Labs at https://github.com/Spomky-Labs/otphp which has been improved and is currently maintained. ## Quick overview of using One Time Passwords on your phone * OTP's involve a shared secret, stored both on the phone and the server * OTP's can be generated on a phone without internet connectivity(AT&T mode) * OTP's should always be used as a second factor of authentication(if your phone is lost, you account is still secured with a password) * Google Authenticator allows you to store multiple OTP secrets and provision those using a QR Code(no more typing in the secret) ## Installation clone this repository and include lib/otphp.php in your project. ## Use ### Time based OTP's $totp = new \OTPHP\TOTP("base32secret3232"); $totp->now(); // => 492039 // OTP verified for current time $totp->verify(492039); // => true //30s later $totp->verify(492039); // => false ### Counter based OTP's $hotp = new \OTPHP\HOTP("base32secretkey3232"); $hotp->at(0); // => 260182 $hotp->at(1); // => 55283 $hotp->at(1401); // => 316439 // OTP verified with a counter $totp->verify(316439, 1401); // => true $totp->verify(316439, 1402); // => false ### Google Authenticator Compatible The library works with the Google Authenticator iPhone and Android app, and also includes the ability to generate provisioning URI's for use with the QR Code scanner built into the app. $totp->provisioning_uri(); // => 'otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP' $hotp->provisioning_uri(); // => 'otpauth://hotp/alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=0' This can then be rendered as a QR Code which can then be scanned and added to the users list of OTP credentials. #### Working example Scan the following barcode with your phone, using Google Authenticator ![QR Code for OTP](http://chart.apis.google.com/chart?cht=qr&chs=250x250&chl=otpauth%3A%2F%2Ftotp%2Falice%40google.com%3Fsecret%3DJBSWY3DPEHPK3PXP) Now run the following and compare the output now(); ## Licence This software is release under MIT licence. ================================================ FILE: lib/hotp.php ================================================ generateOTP($count); } /** * Verify if a password is valid for a specific counter value * * @param integer $otp the one-time password * @param integer $counter the counter value * @return bool true if the counter is valid, false otherwise */ public function verify($otp, $counter) { return ($otp == $this->at($counter)); } /** * Returns the uri for a specific secret for hotp method. * Can be encoded as a image for simple configuration in * Google Authenticator. * * @param string $name the name of the account / profile * @param integer $initial_count the initial counter * @return string the uri for the hmac secret */ public function provisioning_uri($name, $initial_count) { return "otpauth://hotp/".urlencode($name)."?secret={$this->secret}&counter=$initial_count"; } } } ================================================ FILE: lib/otp.php ================================================ digits = isset($opt['digits']) ? $opt['digits'] : 6; $this->digest = isset($opt['digest']) ? $opt['digest'] : 'sha1'; $this->secret = $secret; } /** * Generate a one-time password * * @param integer $input : number used to seed the hmac hash function. * This number is usually a counter (HOTP) or calculated based on the current * timestamp (see TOTP class). * @return integer the one-time password */ public function generateOTP($input) { $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); } $offset = $hmac[19] & 0xf; $code = ($hmac[$offset+0] & 0x7F) << 24 | ($hmac[$offset + 1] & 0xFF) << 16 | ($hmac[$offset + 2] & 0xFF) << 8 | ($hmac[$offset + 3] & 0xFF); return $code % pow(10, $this->digits); } /** * Returns the binary value of the base32 encoded secret * @access private * This method should be private but was left public for * phpunit tests to work. * @return binary secret key */ public function byteSecret() { return \Base32::decode($this->secret); } /** * Turns an integer in a OATH bytestring * @param integer $int * @access private * @return string bytestring */ public function intToBytestring($int) { $result = Array(); while($int != 0) { $result[] = chr($int & 0xFF); $int >>= 8; } return str_pad(join(array_reverse($result)), 8, "\000", STR_PAD_LEFT); } } } ================================================ FILE: lib/otphp.php ================================================ interval = isset($opt['interval']) ? $opt['interval'] : 30; parent::__construct($s, $opt); } /** * Get the password for a specific timestamp value * * @param integer $timestamp the timestamp which is timecoded and * used to seed the hmac hash function. * @return integer the One Time Password */ public function at($timestamp) { return $this->generateOTP($this->timecode($timestamp)); } /** * Get the password for the current timestamp value * * @return integer the current One Time Password */ public function now() { return $this->generateOTP($this->timecode(time())); } /** * Verify if a password is valid for a specific counter value * * @param integer $otp the one-time password * @param integer $timestamp the timestamp for the a given time, defaults to current time. * @return bool true if the counter is valid, false otherwise */ public function verify($otp, $timestamp = null) { if($timestamp === null) $timestamp = time(); return ($otp == $this->at($timestamp)); } /** * Returns the uri for a specific secret for totp method. * Can be encoded as a image for simple configuration in * Google Authenticator. * * @param string $name the name of the account / profile * @return string the uri for the hmac secret */ public function provisioning_uri($name) { return "otpauth://totp/".urlencode($name)."?secret={$this->secret}"; } /** * Transform a timestamp in a counter based on specified internal * * @param integer $timestamp * @return integer the timecode */ protected function timecode($timestamp) { return (int)( (((int)$timestamp * 1000) / ($this->interval * 1000))); } } } ================================================ FILE: tests/HOTPTest.php ================================================ assertEquals(855783,$o->at(0)); $this->assertEquals(549607,$o->at(500)); $this->assertEquals(654666,$o->at(1500)); } public function test_it_verify_the_code() { $o = new \OTPHP\HOTP('JDDK4U6G3BJLEZ7Y'); $this->assertTrue($o->verify(855783, 0)); $this->assertTrue($o->verify(549607, 500)); $this->assertTrue($o->verify(654666, 1500)); } public function test_it_returns_the_provisioning_uri() { $o = new \OTPHP\HOTP('JDDK4U6G3BJLEZ7Y'); $this->assertEquals("otpauth://hotp/name?secret=JDDK4U6G3BJLEZ7Y&counter=0", $o->provisioning_uri('name', 0)); } } ================================================ FILE: tests/OTPTest.php ================================================ assertEquals("H\306\256S\306\330R\262g\370", $o->byteSecret()); } public function test_it_turns_an_int_into_bytestring() { $o = new \OTPHP\OTP('JDDK4U6G3BJLEZ7Y'); $this->assertEquals("\000\000\000\000\000\000\000\000", $o->intToBytestring(0)); $this->assertEquals("\000\000\000\000\000\000\000\001", $o->intToBytestring(1)); $this->assertEquals("\000\000\000\000\000\000\001\364", $o->intToBytestring(500)); $this->assertEquals("\000\000\000\000\000\000\005\334", $o->intToBytestring(1500)); } public function test_it_generate_otp() { $o = new \OTPHP\OTP('JDDK4U6G3BJLEZ7Y'); $this->assertEquals(855783, $o->generateOTP(0)); $this->assertEquals(549607, $o->generateOTP(500)); $this->assertEquals(654666, $o->generateOTP(1500)); } } ================================================ FILE: tests/TOTPTest.php ================================================ assertEquals(30,$o->interval); $b = new \OTPHP\TOTP('JDDK4U6G3BJLEZ7Y', Array('interval'=>60)); $this->assertEquals(60,$b->interval); } public function test_it_gets_the_good_code_at_given_times() { $o = new \OTPHP\TOTP('JDDK4U6G3BJLEZ7Y'); $this->assertEquals(855783,$o->at(0)); $this->assertEquals(762124,$o->at(319690800)); $this->assertEquals(139664,$o->at(1301012137)); } public function test_it_verify_the_code() { $o = new \OTPHP\TOTP('JDDK4U6G3BJLEZ7Y'); $this->assertTrue($o->verify(855783, 0)); $this->assertTrue($o->verify(762124, 319690800)); $this->assertTrue($o->verify(139664, 1301012137)); } public function test_it_returns_the_provisioning_uri() { $o = new \OTPHP\TOTP('JDDK4U6G3BJLEZ7Y'); $this->assertEquals("otpauth://totp/name?secret=JDDK4U6G3BJLEZ7Y", $o->provisioning_uri('name')); } } ================================================ FILE: tests/TestTest.php ================================================ assertEquals(1,1); } } ================================================ FILE: vendor/base32.php ================================================ '0', 'B'=>'1', 'C'=>'2', 'D'=>'3', 'E'=>'4', 'F'=>'5', 'G'=>'6', 'H'=>'7', 'I'=>'8', 'J'=>'9', 'K'=>'10', 'L'=>'11', 'M'=>'12', 'N'=>'13', 'O'=>'14', 'P'=>'15', 'Q'=>'16', 'R'=>'17', 'S'=>'18', 'T'=>'19', 'U'=>'20', 'V'=>'21', 'W'=>'22', 'X'=>'23', 'Y'=>'24', 'Z'=>'25', '2'=>'26', '3'=>'27', '4'=>'28', '5'=>'29', '6'=>'30', '7'=>'31' ); /** * Use padding false when encoding for urls * * @return base32 encoded string * @author Bryan Ruiz **/ public static function encode($input, $padding = true) { if(empty($input)) return ""; $input = str_split($input); $binaryString = ""; for($i = 0; $i < count($input); $i++) { $binaryString .= str_pad(base_convert(ord($input[$i]), 10, 2), 8, '0', STR_PAD_LEFT); } $fiveBitBinaryArray = str_split($binaryString, 5); $base32 = ""; $i=0; while($i < count($fiveBitBinaryArray)) { $base32 .= self::$map[base_convert(str_pad($fiveBitBinaryArray[$i], 5,'0'), 2, 10)]; $i++; } if($padding && ($x = strlen($binaryString) % 40) != 0) { if($x == 8) $base32 .= str_repeat(self::$map[32], 6); else if($x == 16) $base32 .= str_repeat(self::$map[32], 4); else if($x == 24) $base32 .= str_repeat(self::$map[32], 3); else if($x == 32) $base32 .= self::$map[32]; } return $base32; } public static function decode($input) { if(empty($input)) return; $paddingCharCount = substr_count($input, self::$map[32]); $allowedValues = array(6,4,3,1,0); if(!in_array($paddingCharCount, $allowedValues)) return false; for($i=0; $i<4; $i++){ if($paddingCharCount == $allowedValues[$i] && substr($input, -($allowedValues[$i])) != str_repeat(self::$map[32], $allowedValues[$i])) return false; } $input = str_replace('=','', $input); $input = str_split($input); $binaryString = ""; for($i=0; $i < count($input); $i = $i+8) { $x = ""; if(!in_array($input[$i], self::$map)) return false; for($j=0; $j < 8; $j++) { $x .= str_pad(base_convert(@self::$flippedMap[@$input[$i + $j]], 10, 2), 5, '0', STR_PAD_LEFT); } $eightBits = str_split($x, 8); for($z = 0; $z < count($eightBits); $z++) { $binaryString .= ( ($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48 ) ? $y:""; } } return $binaryString; } } ================================================ FILE: vendor/libs.php ================================================