summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/HashLibrary.php
blob: b2720de53a38460af1183b6fee4e34e5eb562054 (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
<?php

// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
class Scribunto_LuaHashLibrary extends Scribunto_LuaLibraryBase {

	public function register() {
		$lib = [
			'listAlgorithms' => [ $this, 'listAlgorithms' ],
			'hashValue' => [ $this, 'hashValue' ],
		];

		return $this->getEngine()->registerInterface( 'mw.hash.lua', $lib );
	}

	/**
	 * Returns a list of known/ supported hash algorithms
	 *
	 * @return string[][]
	 */
	public function listAlgorithms() {
		$algos = hash_algos();
		$algos = array_combine( range( 1, count( $algos ) ), $algos );

		return [ $algos ];
	}

	/**
	 * Hash a given value.
	 *
	 * @param string $algo
	 * @param string $value
	 * @return string[]
	 */
	public function hashValue( $algo, $value ) {
		if ( !in_array( $algo, hash_algos() ) ) {
			throw new Scribunto_LuaError( "Unknown hashing algorithm: $algo" );
		}

		$hash = hash( $algo, $value );

		return [ $hash ];
	}

}