mirror of
https://github.com/danog/tgseclib.git
synced 2025-01-21 21:41:14 +01:00
[issue/39] Extend tests to cover other modes. Also IVs, keys, etc.
This commit is contained in:
parent
f15bc38520
commit
6e597bfe25
@ -7,20 +7,65 @@
|
||||
|
||||
class Crypt_AES_ContinuousBufferTest extends Crypt_AES_TestCase
|
||||
{
|
||||
// https://github.com/phpseclib/phpseclib/issues/39
|
||||
public function testGithubIssue39EncryptDecrypt()
|
||||
// String intented
|
||||
protected $modes = array(
|
||||
'CRYPT_AES_MODE_CTR',
|
||||
'CRYPT_AES_MODE_OFB',
|
||||
'CRYPT_AES_MODE_CFB',
|
||||
);
|
||||
|
||||
protected $plaintexts = array(
|
||||
'',
|
||||
'12345678901234567', // https://github.com/phpseclib/phpseclib/issues/39
|
||||
"\xDE\xAD\xBE\xAF",
|
||||
':-):-):-):-):-):-)', // https://github.com/phpseclib/phpseclib/pull/43
|
||||
);
|
||||
|
||||
protected $ivs = array(
|
||||
'',
|
||||
'test123',
|
||||
);
|
||||
|
||||
protected $keys = array(
|
||||
'',
|
||||
':-8', // https://github.com/phpseclib/phpseclib/pull/43
|
||||
'FOOBARZ',
|
||||
);
|
||||
|
||||
/**
|
||||
* Produces all combinations of test values.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function allCombinations()
|
||||
{
|
||||
$aes = new Crypt_AES(CRYPT_AES_MODE_CFB);
|
||||
$result = array();
|
||||
|
||||
foreach ($this->modes as $mode)
|
||||
foreach ($this->plaintexts as $plaintext)
|
||||
foreach ($this->ivs as $iv)
|
||||
foreach ($this->keys as $key)
|
||||
$result[] = array($mode, $plaintext, $iv, $key);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider allCombinations
|
||||
*/
|
||||
public function testEncryptDecrypt($mode, $plaintext, $iv, $key)
|
||||
{
|
||||
$aes = new Crypt_AES(constant($mode));
|
||||
$aes->enableContinuousBuffer();
|
||||
$aes->setIV($iv);
|
||||
$aes->setKey($key);
|
||||
|
||||
$expected = '12345678901234567';
|
||||
$actual = '';
|
||||
|
||||
for ($i = 0, $strlen = strlen($expected); $i < $strlen; ++$i)
|
||||
for ($i = 0, $strlen = strlen($plaintext); $i < $strlen; ++$i)
|
||||
{
|
||||
$actual .= $aes->decrypt($aes->encrypt($expected[$i]));
|
||||
$actual .= $aes->decrypt($aes->encrypt($plaintext[$i]));
|
||||
}
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
$this->assertEquals($plaintext, $actual);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user