3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\Crypt;
6 use PHPUnit\Framework\TestCase;
9 * Tests random byte generation.
13 * @coversDefaultClass \Drupal\Component\Utility\Crypt
15 class CryptTest extends TestCase {
18 * Tests random byte generation.
20 * @covers ::randomBytes
22 * @see \Drupal\Tests\Component\Utility\CryptRandomFallbackTest::testRandomBytesFallback
24 public function testRandomBytes() {
25 for ($i = 1; $i < 10; $i++) {
26 $count = rand(10, 10000);
27 // Check that different values are being generated.
28 $this->assertNotEquals(Crypt::randomBytes($count), Crypt::randomBytes($count));
30 $this->assertEquals(strlen(Crypt::randomBytes($count)), $count);
35 * Tests hash generation.
37 * @dataProvider providerTestHashBase64
38 * @covers ::hashBase64
42 * @param string $expected_hash
43 * Expected result from hashing $data.
45 public function testHashBase64($data, $expected_hash) {
46 $hash = Crypt::hashBase64($data);
47 $this->assertEquals($expected_hash, $hash, 'The correct hash was not calculated.');
51 * Tests HMAC generation.
53 * @dataProvider providerTestHmacBase64
54 * @covers ::hmacBase64
59 * Key to use in hashing process.
60 * @param string $expected_hmac
61 * Expected result from hashing $data using $key.
63 public function testHmacBase64($data, $key, $expected_hmac) {
64 $hmac = Crypt::hmacBase64($data, $key);
65 $this->assertEquals($expected_hmac, $hmac, 'The correct hmac was not calculated.');
69 * Tests the hmacBase64 method with invalid parameters.
71 * @dataProvider providerTestHmacBase64Invalid
72 * @covers ::hmacBase64
77 * Key to use in hashing process.
79 public function testHmacBase64Invalid($data, $key) {
80 if (method_exists($this, 'expectException')) {
81 $this->expectException('InvalidArgumentException');
84 $this->setExpectedException('InvalidArgumentException');
86 Crypt::hmacBase64($data, $key);
90 * Provides data for self::testHashBase64().
92 * @return array Test data.
94 public function providerTestHashBase64() {
97 '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.',
98 'expectedHash' => '034rT6smZAVRxpq8O98cFFNLIVx_Ph1EwLZQKcmRR_s',
101 'data' => 'SHA-256 is one of the successor hash functions to SHA-1, and is one of the strongest hash functions available.',
102 'expected_hash' => 'yuqkDDYqprL71k4xIb6K6D7n76xldO4jseRhEkEE6SI',
108 * Provides data for self::testHmacBase64().
110 * @return array Test data.
112 public function providerTestHmacBase64() {
115 'data' => 'Calculates a base-64 encoded, URL-safe sha-256 hmac.',
116 'key' => 'secret-key',
117 'expected_hmac' => '2AaH63zwjhekWZlEpAiufyfhAHIzbQhl9Hd9oCi3_c8',
123 * Provides data for self::testHmacBase64().
125 * @return array Test data.
127 public function providerTestHmacBase64Invalid() {
129 [new \stdClass(), new \stdClass()],
130 [new \stdClass(), 'string'],
131 [new \stdClass(), 1],
132 [new \stdClass(), 0],
133 [NULL, new \stdClass()],
134 ['string', new \stdClass()],
135 [1, new \stdClass()],
136 [0, new \stdClass()],