|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\Service; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\Core\Core; |
|
17
|
|
|
use Romm\Formz\Domain\Repository\FormMetadataRepository; |
|
18
|
|
|
use Romm\Formz\Service\Traits\SelfInstantiateTrait; |
|
19
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
20
|
|
|
|
|
21
|
|
|
class HashService implements SingletonInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use SelfInstantiateTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $value |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getHash($value) |
|
30
|
|
|
{ |
|
31
|
|
|
return hash('sha256', $value); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param int $length the number of bytes to generate |
|
36
|
|
|
* @return string the generated random bytes |
|
37
|
|
|
* @throws \Exception |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getUniqueHash($length = 32) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!is_int($length) || $length < 1) { |
|
42
|
|
|
throw new \Exception('Invalid $length parameter'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @var FormMetadataRepository $formMetadataRepository */ |
|
46
|
|
|
$formMetadataRepository = Core::instantiate(FormMetadataRepository::class); |
|
47
|
|
|
|
|
48
|
|
|
do { |
|
49
|
|
|
// Generated random bytes |
|
50
|
|
|
$hash = bin2hex(random_bytes($length)); |
|
51
|
|
|
|
|
52
|
|
|
// Check if the hash has already been generated |
|
53
|
|
|
$object = $formMetadataRepository->findOneByHash($hash); |
|
54
|
|
|
|
|
55
|
|
|
} while ($object !== null); |
|
56
|
|
|
|
|
57
|
|
|
return $hash; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|