This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
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
5 changed files
with
125 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
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,30 @@ | ||
<?php | ||
|
||
namespace Accompli\Utility; | ||
|
||
/** | ||
* Generatator to generate secrets. | ||
* | ||
* @author Ron Rademaker | ||
*/ | ||
class SecretGenerator implements ValueGeneratorInterface | ||
{ | ||
/** | ||
* Generated values to prevent different secrets for the same key. | ||
* | ||
* @var array | ||
*/ | ||
private $generatedValues = array(); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate($key) | ||
{ | ||
if (!array_key_exists($key, $this->generatedValues)) { | ||
$this->generatedValues[$key] = sha1(uniqid()); | ||
} | ||
|
||
return $this->generatedValues[$key]; | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Accompli\Utility; | ||
|
||
/** | ||
* Interface defining a generator for values. | ||
* | ||
* @author Ron Rademaker | ||
*/ | ||
interface ValueGeneratorInterface | ||
{ | ||
/** | ||
* Generate a value for $key. | ||
* | ||
* @param string $key | ||
* | ||
* @return string | ||
*/ | ||
public function generate($key); | ||
} |
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
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,28 @@ | ||
<?php | ||
|
||
namespace Accompli\Test\Utility; | ||
|
||
use Accompli\Utility\SecretGenerator; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
/** | ||
* Unit test for the secret generator. | ||
* | ||
* @author Ron Rademaker | ||
*/ | ||
class SecretGeneratorTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Tests if generated values are not regenerated within the same process. | ||
*/ | ||
public function testGeneratedValuesAreNotRegenerated() | ||
{ | ||
$generator = new SecretGenerator(); | ||
|
||
$generated = $generator->generate('foobar'); | ||
|
||
$this->assertNotEmpty($generated); | ||
$this->assertEquals($generated, $generator->generate('foobar')); | ||
$this->assertNotEquals($generated, $generator->generate('baz')); | ||
} | ||
} |