forked from deceze/Kunststube-CSRFP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoProvider.php
More file actions
43 lines (32 loc) · 1.23 KB
/
Copy pathCryptoProvider.php
File metadata and controls
43 lines (32 loc) · 1.23 KB
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
<?php
namespace Kunststube\CSRFP;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'ICryptoProvider.php';
class CryptoProvider implements ICryptoProvider {
public function getRandomHexString($length) {
try {
return $this->getRandomHexStringFromDevRandom($length);
} catch (\RuntimeException $e) {
trigger_error($e->getMessage() . ' Falling back to internal generator.', E_USER_NOTICE);
return $this->getRandomHexStringFromMtRand($length);
}
}
protected function getRandomHexStringFromDevRandom($length) {
static $sources = array('/dev/urandom', '/dev/random');
foreach ($sources as $source) {
if (@is_readable($source)) {
return bin2hex(file_get_contents($source, false, null, -1, $length / 2));
}
}
throw new \RuntimeException('No system source for randomness available.');
}
protected function getRandomHexStringFromMtRand($length) {
$hex = null;
for ($i = 0; $i < $length; $i++) {
$hex .= base_convert(mt_rand(0, 15), 10, 16);
}
return $hex;
}
public function hash($data, $secret) {
return hash_hmac('sha512', $data, $secret);
}
}