Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
HashLibrary
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 listAlgorithms
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 hashValue
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
4
5class 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     * Returns a list of known/ supported hash algorithms
18     *
19     * @internal
20     * @return string[][]
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     * Hash a given value.
31     *
32     * @internal
33     * @param string $algo
34     * @param string $value
35     * @return string[]
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}