-
Notifications
You must be signed in to change notification settings - Fork 12
/
PKCSKeyGenerator.php
58 lines (49 loc) · 1.21 KB
/
PKCSKeyGenerator.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
<?php
namespace PBEWithMD5AndDES;
class PKCSKeyGenerator
{
protected $key;
protected $iv;
/**
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Gets the initialization vector.
*
* @return string
*/
public function getIv()
{
return $this->iv;
}
/**
* @param string $keyString key string
* @param string $salt salt string
* @param int $iterationsMD5 number of MF5 iterations
* @param int $segments number of segments
*/
public function __construct($keyString, $salt, $iterationsMD5, $segments)
{
$salt = pack('H*', $salt);
$keyMaterial = '';
$data = $keyString . $salt;
$result = '';
for ($j = 0; $j < $segments; $j++) {
if ($j == 0) {
$result = $data;
} else {
$result .= $data;
}
for ($i = 0; $i < $iterationsMD5; $i++) {
$result = md5($result, true);
}
$keyMaterial .= $result;
}
$this->key = substr($keyMaterial, 0, 8);
$this->iv = substr($keyMaterial, 8, 8);
}
}