diff --git a/core/tests/Drupal/Tests/Component/Utility/CryptTest.php b/core/tests/Drupal/Tests/Component/Utility/CryptTest.php index 7eb10ce..fca79ac 100644 --- a/core/tests/Drupal/Tests/Component/Utility/CryptTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/CryptTest.php @@ -37,4 +37,53 @@ public function testRandomBytes() { } } + /** + * Tests \Drupal\Component\Utility\Crypt::hashBase64(). + * + * @dataProvider providerTestHashBase64 + */ + public function testHashBase64($data, $expectedHash) { + $hash = Crypt::hashBase64($data); + $this->assertEquals($expectedHash, $hash, 'The correct hash was not calculated.'); + } + + /** + * Tests \Drupal\Component\Utility\Crypt::hmacBase64(). + * + * @dataProvider providerTestHmacBase64 + */ + public function testHmacBase64($data, $key, $expectedHmac) { + $hmac = Crypt::hmacBase64($data, $key); + $this->assertEquals($expectedHmac, $hmac, 'The correct hmac was not calculated.'); + } + + /** + * Provides data for self::testHashBase64(). + */ + public static function providerTestHashBase64() { + return array( + array( + 'data' => 'The SHA (Secure Hash Algorithm) is one of a number of cryptographic hash functions. A cryptographic hash is like a signature for a text or a data file. SHA-256 algorithm generates an almost-unique, fixed size 256-bit (32-byte) hash. Hash is a one way function – it cannot be decrypted back. This makes it suitable for password validation, challenge hash authentication, anti-tamper, digital signatures.', + 'expectedHash' => '034rT6smZAVRxpq8O98cFFNLIVx_Ph1EwLZQKcmRR_s', + ), + array( + 'data' => 'SHA-256 is one of the successor hash functions to SHA-1, and is one of the strongest hash functions available.', + 'expectedHash' => 'yuqkDDYqprL71k4xIb6K6D7n76xldO4jseRhEkEE6SI', + ), + ); + } + + /** + * Provides data for self::testHmacBase64(). + */ + public static function providerTestHmacBase64() { + return array( + array( + 'data' => 'Calculates a base-64 encoded, URL-safe sha-256 hmac.', + 'key' => 'secret-key', + 'expectedHmac' => '2AaH63zwjhekWZlEpAiufyfhAHIzbQhl9Hd9oCi3_c8', + ), + ); + } + }