Menu

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

Download this file

100 lines (96 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
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| DHParams.java |
| |
| Release 3.0.0 beta 5 |
| Copyright (c) 2005-2007 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 2.1 as published by the Free Software Foundation |
| and appearing in the included file LICENSE. |
| |
\**********************************************************/
/* Diffie-Hellman Parameters for PHPRPC.
*
* Copyright (C) 2007 Ma Bingyao <andot@ujn.edu.cn>
* Version: 3.0.0 beta 5
* LastModified: Apr 24, 2007
* This library is free. You can redistribute it and/or modify it.
*/
package org.phprpc.util;
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Random;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.net.URLDecoder;
final public 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();
JarFile jarFile = new JarFile(URLDecoder.decode(DHParams.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"));
for (int i = 0, n = lengths.length; i < n; i++) {
JarEntry entry = jarFile.getJarEntry("dhparams/" + lengths[i] + ".dhp");
byte[] data = new byte[(int)entry.getSize()];
BufferedInputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
in.read(data);
in.close();
HashMap[] dhParams = (HashMap[])phpser.unserialize(data, HashMap[].class);
dhParamsGen.put(new Integer(lengths[i]), dhParams);
}
}
}
catch (Exception e) {}
}
synchronized 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];
}
synchronized 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);
}
synchronized public int getL() {
return length;
}
synchronized public BigInteger getP() {
return new BigInteger(Cast.toString(dhParams.get("p")));
}
synchronized public BigInteger getG() {
return new BigInteger(Cast.toString(dhParams.get("g")));
}
synchronized public BigInteger getX() {
return (new BigInteger(length - 1, new Random())).setBit(length - 2);
}
synchronized 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.