Menu

[r559]: / phprpc_3.0 / java / DHParams.java  Maximize  Restore  History

Download this file

111 lines (106 with data), 4.3 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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| DHParams.java |
| |
| Release 3.0.2 |
| 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 Lesser General Public License (LGPL) |
| version 3.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: 3.0.2
* LastModified: Feb 11, 2009
* This library is free. You can redistribute it and/or modify it.
*/
package org.phprpc.util;
import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Random;
public final class DHParams {
private int length;
private HashMap dhParams;
private static final int[] lengths = {96, 128, 160, 192, 256, 512, 768, 1024, 1536, 2048, 3072, 4096};
private static final HashMap dhParamsGen = new HashMap();
static {
try {
synchronized (dhParamsGen) {
PHPSerializer phpser = new PHPSerializer();
for (int i = 0, n = lengths.length; i < n; i++) {
String path = "/dhparams/" + lengths[i] + ".dhp";
byte[] data = getBinaryFileFromJar(path);
HashMap[] dhParams = (HashMap[])phpser.unserialize(data, HashMap[].class);
dhParamsGen.put(new Integer(lengths[i]), dhParams);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] getBinaryFileFromJar(String path) throws IOException {
InputStream is = DHParams.class.getResourceAsStream(path);
if (is != null) {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
int read = 0;
while ((read = is.read(bytes)) >= 0) {
bs.write(bytes, 0, read);
}
return bs.toByteArray();
}
return null;
}
public static int getNearest(int n) {
int j = 0;
int m = Math.abs(lengths[0] - n);
for (int i = 1; i < lengths.length; i++) {
int t = Math.abs(lengths[i] - n);
if (m > t) {
m = t;
j = i;
}
}
return lengths[j];
}
public static HashMap getDHParams(int len) {
HashMap[] dhParams = (HashMap[])dhParamsGen.get(new Integer(len));
return dhParams[(int)Math.floor(Math.random() * dhParams.length)];
}
public DHParams(int len) {
length = DHParams.getNearest(len);
dhParams = DHParams.getDHParams(length);
}
public int getL() {
return length;
}
public BigInteger getP() {
return new BigInteger(Cast.toString(dhParams.get("p")));
}
public BigInteger getG() {
return new BigInteger(Cast.toString(dhParams.get("g")));
}
public BigInteger getX() {
return (new BigInteger(length - 1, new Random())).setBit(length - 2);
}
public HashMap getDHParams() {
return dhParams;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.