This repository has been archived by the owner on Mar 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Otis package. | ||
* | ||
* Copyright © 2013 Erin Millard | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Eloquent\Otis\Motp\Generator; | ||
|
||
use Icecave\Isolator\Isolator; | ||
|
||
/** | ||
* Generates mOTP values. | ||
*/ | ||
class MotpGenerator implements MotpGeneratorInterface | ||
{ | ||
/** | ||
* Construct a new mOTP generator. | ||
* | ||
* @param Isolator|null $isolator The isolator to use. | ||
*/ | ||
public function __construct(Isolator $isolator = null) | ||
{ | ||
$this->isolator = Isolator::get($isolator); | ||
} | ||
|
||
/** | ||
* Generate an mOTP value. | ||
* | ||
* @link http://motp.sourceforge.net/#1.1 | ||
* | ||
* @param string $secret The shared secret. | ||
* @param string $pin The PIN. | ||
* @param integer|null $time The Unix timestamp to generate the password for. | ||
* | ||
* @return string The generated mOTP value. | ||
*/ | ||
public function generate($secret, $pin, $time = null) | ||
{ | ||
if (null === $time) { | ||
$time = $this->isolator()->time(); | ||
} | ||
|
||
return substr( | ||
md5(strval(intval($time / 10)) . bin2hex($secret) . $pin), | ||
0, | ||
6 | ||
); | ||
} | ||
|
||
/** | ||
* Get the isolator. | ||
* | ||
* @return Isolator The isolator. | ||
*/ | ||
protected function isolator() | ||
{ | ||
return $this->isolator; | ||
} | ||
|
||
private $isolator; | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Eloquent/Otis/Motp/Generator/MotpGeneratorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Otis package. | ||
* | ||
* Copyright © 2013 Erin Millard | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Eloquent\Otis\Motp\Generator; | ||
|
||
/** | ||
* The interface implemented by mOTP generators. | ||
*/ | ||
interface MotpGeneratorInterface | ||
{ | ||
/** | ||
* Generate an mOTP value. | ||
* | ||
* @link http://motp.sourceforge.net/#1.1 | ||
* | ||
* @param string $secret The shared secret. | ||
* @param string $pin The PIN. | ||
* @param integer|null $time The Unix timestamp to generate the password for. | ||
* | ||
* @return string The generated mOTP value. | ||
*/ | ||
public function generate($secret, $pin, $time = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
test/suite/Eloquent/Otis/Motp/Generator/MotpGeneratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Otis package. | ||
* | ||
* Copyright © 2013 Erin Millard | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Eloquent\Otis\Motp\Generator; | ||
|
||
use Icecave\Isolator\Isolator; | ||
use PHPUnit_Framework_TestCase; | ||
use Phake; | ||
|
||
class MotpGeneratorTest extends PHPUnit_Framework_TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->isolator = Phake::mock(Isolator::className()); | ||
$this->generator = new MotpGenerator($this->isolator); | ||
} | ||
|
||
public function generateData() | ||
{ | ||
// secret pin time expected | ||
return array( | ||
'Known good value 1' => array('12345678', '1234', 0, 'd64c7d'), | ||
'Known good value 2' => array('12345678', '1234', 9, 'd64c7d'), | ||
'Known good value 3' => array('12345678', '1234', 1234, '6bfed1'), | ||
'Known good value 4' => array('12345678', '1234', 1240, 'ee20ed'), | ||
'Known good value 5' => array(pack('H*', 'fd85e62d9beb4542'), '1234', 1379424174, '65e5e8'), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider generateData | ||
*/ | ||
public function testGenerate($secret, $pin, $time, $motp) | ||
{ | ||
$result = $this->generator->generate($secret, $pin, $time); | ||
|
||
$this->assertSame($motp, $result); | ||
} | ||
|
||
public function testGenerateCurrentTime() | ||
{ | ||
Phake::when($this->isolator)->time()->thenReturn(1234); | ||
|
||
$this->assertSame('6bfed1', $this->generator->generate('12345678', '1234', null)); | ||
} | ||
} |