-
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.
Fixed directory on persistent secret key not being created automatically
- Loading branch information
1 parent
dc26265
commit 0fc4a77
Showing
5 changed files
with
176 additions
and
6 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* @project Legatus Crypto | ||
* @link https://github.com/legatus-php/crypto | ||
* @package legatus/crypto | ||
* @author Matias Navarro-Carter mnavarrocarter@gmail.com | ||
* @license MIT | ||
* @copyright 2021 Matias Navarro-Carter | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Legatus\Support; | ||
|
||
/** | ||
* Class FixedRandom. | ||
*/ | ||
final class FixedRandom implements Random | ||
{ | ||
private string $bytes; | ||
|
||
/** | ||
* @param int $bytes | ||
* | ||
* @return FixedRandom | ||
*/ | ||
public static function ofLength(int $bytes): FixedRandom | ||
{ | ||
return self::fromUint8Array(...range(0, $bytes)); | ||
} | ||
|
||
/** | ||
* @param int ...$bytes | ||
* | ||
* @return FixedRandom | ||
*/ | ||
public static function fromUint8Array(int ...$bytes): FixedRandom | ||
{ | ||
return new self(implode('', array_map('chr', $bytes))); | ||
} | ||
|
||
public function __construct(string $bytes) | ||
{ | ||
$this->bytes = $bytes; | ||
} | ||
|
||
/** | ||
* @param int $bytes | ||
* | ||
* @return string | ||
*/ | ||
public function read(int $bytes): string | ||
{ | ||
return substr($this->bytes, 0, $bytes); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* @project Legatus Crypto | ||
* @link https://github.com/legatus-php/crypto | ||
* @package legatus/crypto | ||
* @author Matias Navarro-Carter mnavarrocarter@gmail.com | ||
* @license MIT | ||
* @copyright 2021 Matias Navarro-Carter | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Legatus\Support; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Vfs\FileSystem; | ||
|
||
class SecretKeyTest extends TestCase | ||
{ | ||
public function testItGeneratesASecretKey(): void | ||
{ | ||
$random = FixedRandom::ofLength(32); | ||
$key = SecretKey::generate($random)->toString(); | ||
self::assertSame('AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8', $key); | ||
} | ||
|
||
public function testItGeneratesAPersistentSecretKey(): void | ||
{ | ||
$fs = FileSystem::factory('vfs://'); | ||
$fs->mount(); | ||
|
||
$random = FixedRandom::ofLength(32); | ||
$key = SecretKey::persistent('vfs://data/secret.key', $random)->toString(); | ||
self::assertSame('AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8', $key); | ||
self::assertFileExists('vfs://data/secret.key'); | ||
|
||
$fs->unmount(); | ||
} | ||
} |