mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-25 16:58:35 +00:00
0f4db74148
Provides a simple wrapper for PHP's hash() and hash_algos() functions. I will add docs to the Lua reference manual once this is merged. Bug: T142585 Change-Id: I6697463974a175e99f9b77428a1085247165ebc9
45 lines
965 B
PHP
45 lines
965 B
PHP
<?php
|
|
|
|
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
|
|
class Scribunto_LuaHashLibrary extends Scribunto_LuaLibraryBase {
|
|
|
|
public function register() {
|
|
$lib = array(
|
|
'listAlgorithms' => array( $this, 'listAlgorithms' ),
|
|
'hashValue' => array( $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 array( $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 array( $hash );
|
|
}
|
|
|
|
}
|