summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/utils/MWCryptHashTest.php
blob: 94705bff4855f077b0b4a47782c26808d41e55a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

/**
 * @group Hash
 *
 * @covers MWCryptHash
 */
class MWCryptHashTest extends PHPUnit\Framework\TestCase {

	use MediaWikiCoversValidator;

	public function testHashLength() {
		if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
			$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
		}

		$this->assertEquals( 64, MWCryptHash::hashLength(), 'Raw hash length' );
		$this->assertEquals( 128, MWCryptHash::hashLength( false ), 'Hex hash length' );
	}

	public function testHash() {
		if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
			$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
		}

		$data = 'foobar';
		// phpcs:ignore Generic.Files.LineLength
		$hash = '9923afaec3a86f865bb231a588f453f84e8151a2deb4109aebc6de4284be5bebcff4fab82a7e51d920237340a043736e9d13bab196006dcca0fe65314d68eab9';

		$this->assertEquals(
			hex2bin( $hash ),
			MWCryptHash::hash( $data ),
			'Raw hash'
		);
		$this->assertEquals(
			$hash,
			MWCryptHash::hash( $data, false ),
			'Hex hash'
		);
	}

	public function testHmac() {
		if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
			$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
		}

		$data = 'foobar';
		$key = 'secret';
		// phpcs:ignore Generic.Files.LineLength
		$hash = 'ddc94177b2020e55ce2049199fd9cc6327f416ff6dc621cc34cb43d9bec61d73372b4790c0e24957f565ecaf2d42821e6303619093e99cbe14a3b9250bda5f81';

		$this->assertEquals(
			hex2bin( $hash ),
			MWCryptHash::hmac( $data, $key ),
			'Raw hmac'
		);
		$this->assertEquals(
			$hash,
			MWCryptHash::hmac( $data, $key, false ),
			'Hex hmac'
		);
	}

}