dhparams.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**********************************************************\
  3. | |
  4. | The implementation of PHPRPC Protocol 3.0 |
  5. | |
  6. | dhparams.php |
  7. | |
  8. | Release 3.0.1 |
  9. | Copyright by Team-PHPRPC |
  10. | |
  11. | WebSite: http://www.phprpc.org/ |
  12. | http://www.phprpc.net/ |
  13. | http://www.phprpc.com/ |
  14. | http://sourceforge.net/projects/php-rpc/ |
  15. | |
  16. | Authors: Ma Bingyao <andot@ujn.edu.cn> |
  17. | |
  18. | This file may be distributed and/or modified under the |
  19. | terms of the GNU General Public License (GPL) version |
  20. | 2.0 as published by the Free Software Foundation and |
  21. | appearing in the included file LICENSE. |
  22. | |
  23. \**********************************************************/
  24. /* Diffie-Hellman Parameters for PHPRPC.
  25. *
  26. * Copyright: Ma Bingyao <andot@ujn.edu.cn>
  27. * Version: 1.2
  28. * LastModified: Apr 12, 2010
  29. * This library is free. You can redistribute it and/or modify it under GPL.
  30. */
  31. class DHParams {
  32. var $len;
  33. var $dhParams;
  34. function getNearest($n, $a) {
  35. $j = 0;
  36. $m = abs($a[0] - $n);
  37. for ($i = 1; $i < count($a); $i++) {
  38. $t = abs($a[$i] - $n);
  39. if ($m > $t) {
  40. $m = $t;
  41. $j = $i;
  42. }
  43. }
  44. return $a[$j];
  45. }
  46. function DHParams($len = 128) {
  47. if (extension_loaded('gmp')) {
  48. $a = array(96, 128, 160, 192, 256, 512, 768, 1024, 1536, 2048, 3072, 4096);
  49. }
  50. else if (extension_loaded('big_int')) {
  51. $a = array(96, 128, 160, 192, 256, 512, 768, 1024, 1536);
  52. }
  53. else if (extension_loaded('bcmath')) {
  54. $a = array(96, 128, 160, 192, 256, 512);
  55. }
  56. else {
  57. $a = array(96, 128, 160);
  58. }
  59. $this->len = $this->getNearest($len, $a);
  60. $dhParams = unserialize(file_get_contents("dhparams/{$this->len}.dhp", true));
  61. $this->dhParams = $dhParams[mt_rand(0, count($dhParams) - 1)];
  62. }
  63. function getL() {
  64. return $this->len;
  65. }
  66. function getP() {
  67. return $this->dhParams['p'];
  68. }
  69. function getG() {
  70. return $this->dhParams['g'];
  71. }
  72. function getDHParams() {
  73. return $this->dhParams;
  74. }
  75. }
  76. ?>