|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework, SpiralScout LLC. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Spiral\Tests\Encrypter; |
|
12
|
|
|
|
|
13
|
|
|
use Defuse\Crypto\Key; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Encrypter\Encrypter; |
|
16
|
|
|
use Spiral\Encrypter\Exception\DecryptException; |
|
17
|
|
|
use Spiral\Encrypter\Exception\EncrypterException; |
|
18
|
|
|
|
|
19
|
|
|
class EncrypterTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testImmutable(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$encrypter = new Encrypter($keyA = Key::CreateNewRandomKey()->saveToAsciiSafeString()); |
|
24
|
|
|
$new = $encrypter->withKey($keyB = Key::CreateNewRandomKey()->saveToAsciiSafeString()); |
|
25
|
|
|
|
|
26
|
|
|
$this->assertNotSame($encrypter, $new); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals($keyA, $encrypter->getKey()); |
|
29
|
|
|
$this->assertEquals($keyB, $new->getKey()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @covers \Spiral\Encrypter\Encrypter::encrypt |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testEncryption(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$encrypter = new Encrypter(Key::CreateNewRandomKey()->saveToAsciiSafeString()); |
|
38
|
|
|
|
|
39
|
|
|
$encrypted = $encrypter->encrypt('test string'); |
|
40
|
|
|
$this->assertNotEquals('test string', $encrypted); |
|
41
|
|
|
$this->assertEquals('test string', $encrypter->decrypt($encrypted)); |
|
42
|
|
|
|
|
43
|
|
|
$encrypter = $encrypter->withKey(Key::CreateNewRandomKey()->saveToAsciiSafeString()); |
|
44
|
|
|
|
|
45
|
|
|
$encrypted = $encrypter->encrypt('test string'); |
|
46
|
|
|
$this->assertNotEquals('test string', $encrypted); |
|
47
|
|
|
$this->assertEquals('test string', $encrypter->decrypt($encrypted)); |
|
48
|
|
|
|
|
49
|
|
|
$encrypted = $encrypter->encrypt('test string'); |
|
50
|
|
|
$this->assertNotEquals('test string', $encrypted); |
|
51
|
|
|
$this->assertEquals('test string', $encrypter->decrypt($encrypted)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testBadData(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->expectException(DecryptException::class); |
|
57
|
|
|
|
|
58
|
|
|
$encrypter = new Encrypter(Key::CreateNewRandomKey()->saveToAsciiSafeString()); |
|
59
|
|
|
|
|
60
|
|
|
$encrypted = $encrypter->encrypt('test string'); |
|
61
|
|
|
$this->assertNotEquals('test string', $encrypted); |
|
62
|
|
|
$this->assertEquals('test string', $encrypter->decrypt($encrypted)); |
|
63
|
|
|
|
|
64
|
|
|
$encrypter->decrypt('badData.' . $encrypted); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testBadKey(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->expectException(EncrypterException::class); |
|
70
|
|
|
|
|
71
|
|
|
$encrypter = new Encrypter('bad-key'); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testBadWithKey(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->expectException(EncrypterException::class); |
|
77
|
|
|
|
|
78
|
|
|
$encrypter = new Encrypter(Key::CreateNewRandomKey()->saveToAsciiSafeString()); |
|
79
|
|
|
$encrypter = $encrypter->withKey('bad-key'); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|