Passed
Push — main ( 2cee8a...b41e82 )
by Anatolyi
09:15
created

RngTillHundred   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gambling\Tech\Dice;
6
7
/**
8
 * RNG with PFS for DICE (in the range from 0 to 99.99)
9
 *
10
 * Provably fair system is an algorithm based on technologies that allow online
11
 * randomization to step up and reach a new level of fairness and openness.
12
 *
13
 * More about provably fair system (PFS):
14
 * https://www.provably.com
15
 *
16
 *
17
 * JS alternative:
18
 *
19
 * const luckyNumber = (({
20
 *     serverSeed,
21
 *     clientSeed,
22
 *     nonce
23
 * }) => {
24
 *   const hash = sha512.hmac(serverSeed, `${clientSeed}-${nonce}`);
25
 *   let offset = 0, number = 0;
26
 *   do {
27
 *     number = parseInt(hash.substr(offset, 5), 16);
28
 *     offset += 5;
29
 *   }
30
 *   while (number >= 1000000);
31
 *
32
 *   return (number % 10000) / 100;
33
 * });
34
 */
35
class RngTillHundred
36
{
37
    /**
38
     * Generates a number in the range 0 to 99.99
39
     *
40
     * @param string $serverSeed
41
     * @param string $clientSeed
42
     * @param int $nonce
43
     * @return float
44
     */
45
    public function __invoke(string $serverSeed, string $clientSeed, int $nonce): float
46
    {
47
        $hash = hash_hmac('sha512', "${clientSeed}-${nonce}", $serverSeed);
48
        $offset = 0;
49
50
        do {
51
            $number = hexdec(mb_substr($hash, $offset, 5));
52
            $offset += 5;
53
        } while ($number >= 1000000);
54
55
        return ($number % 10000) / 100;
56
    }
57
}
58