Skip to content
This repository has been archived by the owner on Mar 27, 2019. It is now read-only.

Commit

Permalink
Implemented an mOTP generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Sep 17, 2013
1 parent 4b4c9bb commit 75a3ded
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/Eloquent/Otis/Motp/Generator/MotpGenerator.php
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 src/Eloquent/Otis/Motp/Generator/MotpGeneratorInterface.php
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public function generate(
$secret,
$window = null,
$time = null,
HotpHashAlgorithm $lagorithm = null
HotpHashAlgorithm $algorithm = null
);
}
56 changes: 56 additions & 0 deletions test/suite/Eloquent/Otis/Motp/Generator/MotpGeneratorTest.php
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));
}
}

0 comments on commit 75a3ded

Please sign in to comment.