2016-08-15 22:14:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Scribunto_LuaHashLibrary extends Scribunto_LuaLibraryBase {
|
|
|
|
|
|
|
|
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() ) ) {
|
|
|
|
throw new Scribunto_LuaError( "Unknown hashing algorithm: $algo" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash = hash( $algo, $value );
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $hash ];
|
2016-08-15 22:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|