| 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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
} |