forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptTest.php
110 lines (92 loc) · 3.5 KB
/
CryptTest.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
class CryptTest extends PHPUnit_Framework_TestCase
{
/**
* @requires extension mcrypt
*/
public function testEncryption()
{
$tests = array(
md5(uniqid()) => str_repeat('x', mt_rand(1, 255)),
time().time() => str_shuffle('abcdefeghijklmnopqrst'),
'le$ki12432543543543543' => null
);
$encrypt = new Phalcon\Crypt();
foreach (array(MCRYPT_MODE_ECB, MCRYPT_MODE_CBC, MCRYPT_MODE_CFB, MCRYPT_MODE_CFB, MCRYPT_MODE_NOFB) as $mode) {
$encrypt->setMode($mode);
foreach ($tests as $key => $test) {
$encrypt->setKey(substr($key, 0, 16));
$encryption = $encrypt->encrypt($test);
$this->assertEquals(rtrim($encrypt->decrypt($encryption), "\0"), $test);
}
foreach ($tests as $key => $test) {
$encryption = $encrypt->encrypt($test, substr($key, 0, 16));
$this->assertEquals(rtrim($encrypt->decrypt($encryption, substr($key, 0, 16)), "\0"), $test);
}
}
}
/**
* @requires extension mcrypt
* @medium
*/
public function testPadding()
{
$texts = array('');
$key = '0123456789ABCDEF0123456789ABCDEF';
$modes = array('ecb', 'cbc', 'cfb');
$pads = array(
Phalcon\Crypt::PADDING_ANSI_X_923, Phalcon\Crypt::PADDING_PKCS7,
Phalcon\Crypt::PADDING_ISO_10126, Phalcon\Crypt::PADDING_ISO_IEC_7816_4,
Phalcon\Crypt::PADDING_ZERO, Phalcon\Crypt::PADDING_SPACE
);
for ($i=1; $i<128; ++$i) {
$texts[] = str_repeat('A', $i);
}
$crypt = new Phalcon\Crypt();
$crypt->setCipher(MCRYPT_RIJNDAEL_256)->setKey(substr($key, 0, 16));
foreach ($pads as $padding) {
$crypt->setPadding($padding);
foreach ($modes as $mode) {
$crypt->setMode($mode);
foreach ($texts as $text) {
$encrypted = $crypt->encrypt($text);
$actual = $crypt->decrypt($encrypted);
$this->assertEquals($actual, $text);
}
}
}
}
/**
* @requires extension mcrypt
*/
public function testEncryptBase64()
{
$crypt = new \Phalcon\Crypt();
$crypt->setPadding(\Phalcon\Crypt::PADDING_ANSI_X_923);
$key = substr('phalcon notice 13123123', 0, 16);
$text = 'https://github.com/phalcon/cphalcon/issues?state=open';
$encrypted = $crypt->encryptBase64($text, substr($key, 0, 16));
$actual = $crypt->decryptBase64($encrypted, $key);
$this->assertEquals($actual, $text);
$encrypted = $crypt->encryptBase64($text, $key, TRUE);
$actual = $crypt->decryptBase64($encrypted, $key, TRUE);
$this->assertEquals($actual, $text);
}
}