-
Notifications
You must be signed in to change notification settings - Fork 6
/
Hasher.php
62 lines (49 loc) · 1.29 KB
/
Hasher.php
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
<?php
/**
* Matomo - Open source web analytics
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @copyright (c) 2016 Joubert RedRat
* @author Joubert RedRat <[email protected]>
* @license MIT
* @category Matomo_Plugins
* @package ProtectTrackID
*/
declare(strict_types=1);
namespace Piwik\Plugins\ProtectTrackID;
require_once(__DIR__ . '/vendor/autoload.php');
use Hashids\Hashids;
class Hasher
{
private $hashids;
public function __construct(PluginSettings $settings)
{
$this->hashids = new Hashids($settings->salt, $settings->length, $settings->base);
}
public function encode(string $value): string
{
if (!IdSite::isValid($value)) {
throw InvalidHasherValueException::handleEncode($value);
}
return $this
->hashids
->encode($value)
;
}
public function decode(string $value): int
{
$ids = $this
->hashids
->decode($value)
;
if (count($ids) !== 1) {
throw InvalidHasherValueException::handleDecode($value);
}
if (!IdSite::isValid($ids[0])) {
throw InvalidHasherValueException::handleDecode($value);
}
return $ids[0];
}
}