forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypterTest.php
More file actions
executable file
·96 lines (84 loc) · 3.01 KB
/
EncrypterTest.php
File metadata and controls
executable file
·96 lines (84 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
use Illuminate\Support\Str;
use Illuminate\Encryption\Encrypter;
class EncrypterTest extends PHPUnit_Framework_TestCase
{
public function testEncryption()
{
$e = new Encrypter(str_repeat('a', 16));
$encrypted = $e->encrypt('foo');
$this->assertNotEquals('foo', $encrypted);
$this->assertEquals('foo', $e->decrypt($encrypted));
}
public function testWithCustomCipher()
{
$e = new Encrypter(str_repeat('b', 32), 'AES-256-CBC');
$encrypted = $e->encrypt('bar');
$this->assertNotEquals('bar', $encrypted);
$this->assertEquals('bar', $e->decrypt($encrypted));
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.
*/
public function testDoNoAllowLongerKey()
{
new Encrypter(str_repeat('z', 32));
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.
*/
public function testWithBadKeyLength()
{
new Encrypter(str_repeat('a', 5));
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.
*/
public function testWithBadKeyLengthAlternativeCipher()
{
new Encrypter(str_repeat('a', 16), 'AES-256-CFB8');
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.
*/
public function testWithUnsupportedCipher()
{
new Encrypter(str_repeat('c', 16), 'AES-256-CFB8');
}
/**
* @expectedException Illuminate\Contracts\Encryption\DecryptException
* @expectedExceptionMessage The payload is invalid.
*/
public function testExceptionThrownWhenPayloadIsInvalid()
{
$e = new Encrypter(str_repeat('a', 16));
$payload = $e->encrypt('foo');
$payload = str_shuffle($payload);
$e->decrypt($payload);
}
/**
* @expectedException Illuminate\Contracts\Encryption\DecryptException
* @expectedExceptionMessage The MAC is invalid.
*/
public function testExceptionThrownWithDifferentKey()
{
$a = new Encrypter(str_repeat('a', 16));
$b = new Encrypter(str_repeat('b', 16));
$b->decrypt($a->encrypt('baz'));
}
public function testOpenSslEncrypterCanDecryptMcryptedData()
{
if (! extension_loaded('mcrypt')) {
$this->markTestSkipped('Mcrypt module not installed');
}
$key = Str::random(32);
$encrypter = new Illuminate\Encryption\McryptEncrypter($key);
$encrypted = $encrypter->encrypt('foo');
$openSslEncrypter = new Illuminate\Encryption\Encrypter($key, 'AES-256-CBC');
$this->assertEquals('foo', $openSslEncrypter->decrypt($encrypted));
}
}