Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Token
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hash
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray;
6
7class Token
8{
9    protected string $token;
10    protected string $secret;
11
12    public function __construct(
13        #[\SensitiveParameter]
14        string $secret,
15        #[\SensitiveParameter]
16        ?string $token = null,
17    ) {
18        $this->secret = $secret;
19
20        if ($token === null) {
21            $this->token = bin2hex(random_bytes(16));
22        } else {
23            $this->token = $token;
24        }
25    }
26
27    public function get(): string
28    {
29        return $this->token;
30    }
31
32    public function hash(): string
33    {
34        return hash_hmac('sha256', $this->token, $this->secret);
35    }
36}