Skip to content

Commit

Permalink
Allow multiple instances to not clobber each other. Enforce seed length.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Dec 23, 2015
1 parent 2df8ae4 commit a3dabe8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/SeedSpring.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ class SeedSpring

public function __construct(string $seed = '', int $counter = 0)
{
if (\function_exists('\\mb_strlen')) {
if (\mb_strlen($seed, '8bit') !== 16) {
throw new \InvalidArgumentException('Seed must be 16 bytes');
}
} elseif (\strlen($seed) !== 16) {
throw new \InvalidArgumentException('Seed must be 16 bytes');
}
$this->seed('set', $seed);
$this->counter = 0;
}
Expand All @@ -22,11 +29,12 @@ public function __construct(string $seed = '', int $counter = 0)
private function seed(string $action = 'get', string $data = '')
{
static $seed = null;
$hash = \spl_object_hash($this);
if ($action === 'set') {
$seed = $data;
$seed[$hash] = $data;
return;
} elseif ($action === 'get') {
return $data;
return $seed[$hash];
} else {
throw new \Error(
'Unknown action'
Expand Down

0 comments on commit a3dabe8

Please sign in to comment.