mirror of
https://github.com/phpseclib/phpseclib.git
synced 2025-01-27 00:58:25 +00:00
Merge pull request #18 from bantu/feature/hash-tests
Some Test Infrastructure for Hashing Algorithms and MD5 Tests
This commit is contained in:
commit
ed67b3b68d
47
tests/Crypt/Hash/MD5Test.php
Normal file
47
tests/Crypt/Hash/MD5Test.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @author Andreas Fischer <bantu@phpbb.com>
|
||||||
|
* @copyright MMXII Andreas Fischer
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Crypt_Hash_MD5Test extends Crypt_Hash_TestCase
|
||||||
|
{
|
||||||
|
public function getInstance()
|
||||||
|
{
|
||||||
|
return new Crypt_Hash('md5');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider hashData()
|
||||||
|
*/
|
||||||
|
public function testHash($message, $result)
|
||||||
|
{
|
||||||
|
$this->assertHashesTo($this->getInstance(), $message, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function hashData()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('', 'd41d8cd98f00b204e9800998ecf8427e'),
|
||||||
|
array('The quick brown fox jumps over the lazy dog', '9e107d9d372bb6826bd81d3542a419d6'),
|
||||||
|
array('The quick brown fox jumps over the lazy dog.', 'e4d909c290d0fb1ca068ffaddf22cbd0'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider hmacData()
|
||||||
|
*/
|
||||||
|
public function testHMAC($key, $message, $result)
|
||||||
|
{
|
||||||
|
$this->assertHMACsTo($this->getInstance(), $key, $message, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function hmacData()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array('', '', '74e6f7298a9c2d168935f58c001bad88'),
|
||||||
|
array('key', 'The quick brown fox jumps over the lazy dog', '80070713463e7749b90c2dc24911e275'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
47
tests/Crypt/Hash/TestCase.php
Normal file
47
tests/Crypt/Hash/TestCase.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @author Andreas Fischer <bantu@phpbb.com>
|
||||||
|
* @copyright MMXII Andreas Fischer
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
abstract class Crypt_Hash_TestCase extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
static public function setUpBeforeClass()
|
||||||
|
{
|
||||||
|
require_once('Crypt/Hash.php');
|
||||||
|
|
||||||
|
if (!defined('CRYPT_HASH_MODE'))
|
||||||
|
{
|
||||||
|
define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
if (defined('CRYPT_HASH_MODE') && CRYPT_HASH_MODE !== CRYPT_HASH_MODE_INTERNAL)
|
||||||
|
{
|
||||||
|
$this->markTestSkipped('Skipping test because CRYPT_HASH_MODE is not defined as CRYPT_HASH_MODE_INTERNAL.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function assertHashesTo(Crypt_Hash $hash, $message, $expected)
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
strtolower($expected),
|
||||||
|
bin2hex($hash->hash($message)),
|
||||||
|
sprintf("Failed asserting that '%s' hashes to '%s'.", $message, $expected)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function assertHMACsTo(Crypt_Hash $hash, $key, $message, $expected)
|
||||||
|
{
|
||||||
|
$hash->setKey($key);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
strtolower($expected),
|
||||||
|
bin2hex($hash->hash($message)),
|
||||||
|
sprintf("Failed asserting that '%s' HMACs to '%s' with key '%s'.", $message, $expected, $key)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user