File: engines.inc

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (106 lines) | stat: -rw-r--r-- 2,099 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace Random\Engine\Test;

use Random\Engine;

final class TestShaEngine implements Engine
{
    private string $state;

    public function __construct(?string $state = null)
    {
        if ($state !== null) {
            $this->state = $state;
        } else {
            $this->state = random_bytes(20);
        }
    }

    public function generate(): string
    {
        $this->state = sha1($this->state, true);

        return substr($this->state, 0, 8);
    }
}

final class TestWrapperEngine implements Engine
{
    private int $count = 0;

    public function __construct(private readonly Engine $engine)
    {
    }

    public function generate(): string
    {
        $this->count++;

        return $this->engine->generate();
    }

    public function getCount(): int
    {
        return $this->count;
    }
}

final class TestXoshiro128PlusPlusEngine implements Engine
{
    public function __construct(
        private int $s0,
        private int $s1,
        private int $s2,
        private int $s3
    ) {
    }

    private static function rotl($x, $k)
    {
        return (($x << $k) | ($x >> (32 - $k))) & 0xFFFFFFFF;
    }

    public function generate(): string
    {
        $result = (self::rotl(($this->s0 + $this->s3) & 0xFFFFFFFF, 7) + $this->s0) & 0xFFFFFFFF;

        $t = ($this->s1 << 9)  & 0xFFFFFFFF;

        $this->s2 ^= $this->s0;
        $this->s3 ^= $this->s1;
        $this->s1 ^= $this->s2;
        $this->s0 ^= $this->s3;

        $this->s2 ^= $t;

        $this->s3 = self::rotl($this->s3, 11);

        return pack('V', $result);
    }
}

final class TestCountingEngine32 implements Engine
{
    private int $count = 0;

    public function generate(): string
    {
        return pack('V', $this->count++);
    }
}

final class TestCountingEngine64 implements Engine
{
    private int $count = 0;

    public function generate(): string
    {
        if ($this->count > 2147483647 || $this->count < 0) {
            throw new \Exception('Overflow');
        }
        return pack('V', $this->count++) . "\x00\x00\x00\x00";
    }
}

?>