From 99bfab11a621a16533b1c8a4a42f0547f9ede6f8 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 20 Aug 2012 13:48:33 +0200 Subject: [PATCH 1/2] [feature/hash-tests] Some infrastructure for testing the hash implementations. --- tests/Crypt/Hash/TestCase.php | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/Crypt/Hash/TestCase.php diff --git a/tests/Crypt/Hash/TestCase.php b/tests/Crypt/Hash/TestCase.php new file mode 100644 index 00000000..1489acd7 --- /dev/null +++ b/tests/Crypt/Hash/TestCase.php @@ -0,0 +1,47 @@ + + * @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) + ); + } +} From f63178536534eed62b5d778b94bc552ccc02cdb8 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Mon, 20 Aug 2012 13:49:41 +0200 Subject: [PATCH 2/2] [feature/hash-tests] Tests for the MD5 hash algorithm. --- tests/Crypt/Hash/MD5Test.php | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/Crypt/Hash/MD5Test.php diff --git a/tests/Crypt/Hash/MD5Test.php b/tests/Crypt/Hash/MD5Test.php new file mode 100644 index 00000000..41fc20a9 --- /dev/null +++ b/tests/Crypt/Hash/MD5Test.php @@ -0,0 +1,47 @@ + + * @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'), + ); + } +}