2016-08-15 22:14:29 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-03 10:03:12 +00:00
|
|
|
class Scribunto_LuaHashLibrary extends Scribunto_LuaLibraryBase {
|
2016-08-15 22:14:29 +00:00
|
|
|
|
|
|
|
public function register() {
|
2017-06-15 17:19:00 +00:00
|
|
|
$lib = [
|
|
|
|
'listAlgorithms' => [ $this, 'listAlgorithms' ],
|
|
|
|
'hashValue' => [ $this, 'hashValue' ],
|
|
|
|
];
|
2016-08-15 22:14:29 +00:00
|
|
|
|
|
|
|
return $this->getEngine()->registerInterface( 'mw.hash.lua', $lib );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of known/ supported hash algorithms
|
|
|
|
*
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2016-08-15 22:14:29 +00:00
|
|
|
* @return string[][]
|
|
|
|
*/
|
|
|
|
public function listAlgorithms() {
|
|
|
|
$algos = hash_algos();
|
|
|
|
$algos = array_combine( range( 1, count( $algos ) ), $algos );
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $algos ];
|
2016-08-15 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash a given value.
|
|
|
|
*
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2016-08-15 22:14:29 +00:00
|
|
|
* @param string $algo
|
|
|
|
* @param string $value
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function hashValue( $algo, $value ) {
|
|
|
|
if ( !in_array( $algo, hash_algos() ) ) {
|
2022-08-03 10:03:12 +00:00
|
|
|
throw new Scribunto_LuaError( "Unknown hashing algorithm: $algo" );
|
2016-08-15 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$hash = hash( $algo, $value );
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $hash ];
|
2016-08-15 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|