Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/HashLibrary.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
4
5 class HashLibrary extends LibraryBase {
6
7 public function register() {
8 $lib = [
9 'listAlgorithms' => [ $this, 'listAlgorithms' ],
10 'hashValue' => [ $this, 'hashValue' ],
11 ];
12
13 return $this->getEngine()->registerInterface( 'mw.hash.lua', $lib );
14 }
15
16 /**
17 * Returns a list of known/ supported hash algorithms
18 *
19 * @internal
20 * @return string[][]
21 */
22 public function listAlgorithms() {
23 $algos = hash_algos();
24 $algos = array_combine( range( 1, count( $algos ) ), $algos );
25
26 return [ $algos ];
27 }
28
29 /**
30 * Hash a given value.
31 *
32 * @internal
33 * @param string $algo
34 * @param string $value
35 * @return string[]
36 */
37 public function hashValue( $algo, $value ) {
38 if ( !in_array( $algo, hash_algos() ) ) {
39 throw new LuaError( "Unknown hashing algorithm: $algo" );
40 }
41
42 $hash = hash( $algo, $value );
43
44 return [ $hash ];
45 }
46
47 }