dhparams.php
2.54 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| dhparams.php |
| |
| Release 3.0.1 |
| Copyright by Team-PHPRPC |
| |
| WebSite: http://www.phprpc.org/ |
| http://www.phprpc.net/ |
| http://www.phprpc.com/ |
| http://sourceforge.net/projects/php-rpc/ |
| |
| Authors: Ma Bingyao <andot@ujn.edu.cn> |
| |
| This file may be distributed and/or modified under the |
| terms of the GNU General Public License (GPL) version |
| 2.0 as published by the Free Software Foundation and |
| appearing in the included file LICENSE. |
| |
\**********************************************************/
/* Diffie-Hellman Parameters for PHPRPC.
*
* Copyright: Ma Bingyao <andot@ujn.edu.cn>
* Version: 1.2
* LastModified: Apr 12, 2010
* This library is free. You can redistribute it and/or modify it under GPL.
*/
class DHParams {
var $len;
var $dhParams;
function getNearest($n, $a) {
$j = 0;
$m = abs($a[0] - $n);
for($i = 1; $i < count($a); $i ++) {
$t = abs($a[$i] - $n);
if ($m > $t) {
$m = $t;
$j = $i;
}
}
return $a[$j];
}
function DHParams($len = 128) {
if (extension_loaded('gmp')) {
$a = array(96, 128, 160, 192, 256, 512, 768, 1024, 1536, 2048, 3072, 4096);
} else if (extension_loaded('big_int')) {
$a = array(96, 128, 160, 192, 256, 512, 768, 1024, 1536);
} else if (extension_loaded('bcmath')) {
$a = array(96, 128, 160, 192, 256, 512);
} else {
$a = array(96, 128, 160);
}
$this->len = $this->getNearest($len, $a);
$dhParams = unserialize(file_get_contents("dhparams/{$this->len}.dhp", true));
$this->dhParams = $dhParams[mt_rand(0, count($dhParams) - 1)];
}
function getL() {
return $this->len;
}
function getP() {
return $this->dhParams['p'];
}
function getG() {
return $this->dhParams['g'];
}
function getDHParams() {
return $this->dhParams;
}
}