diff options
Diffstat (limited to 'www/wiki/extensions/Scribunto/includes/engines/LuaCommon/HashLibrary.php')
-rw-r--r-- | www/wiki/extensions/Scribunto/includes/engines/LuaCommon/HashLibrary.php | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/HashLibrary.php b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/HashLibrary.php new file mode 100644 index 00000000..b2720de5 --- /dev/null +++ b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/HashLibrary.php @@ -0,0 +1,44 @@ +<?php + +// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps +class Scribunto_LuaHashLibrary extends Scribunto_LuaLibraryBase { + + public function register() { + $lib = [ + 'listAlgorithms' => [ $this, 'listAlgorithms' ], + 'hashValue' => [ $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 [ $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 [ $hash ]; + } + +} |