001: package org.bouncycastle.crypto.params;
002:
003: import org.bouncycastle.crypto.CipherParameters;
004:
005: import java.math.BigInteger;
006:
007: public class DHParameters implements CipherParameters {
008: private static final int DEFAULT_MINIMUM_LENGTH = 160;
009:
010: private BigInteger g;
011: private BigInteger p;
012: private BigInteger q;
013: private BigInteger j;
014: private int m = DEFAULT_MINIMUM_LENGTH;
015: private int l;
016: private DHValidationParameters validation;
017:
018: public DHParameters(BigInteger p, BigInteger g) {
019: this (p, g, null, DEFAULT_MINIMUM_LENGTH, 0, null, null);
020: }
021:
022: public DHParameters(BigInteger p, BigInteger g, BigInteger q) {
023: this (p, g, q, DEFAULT_MINIMUM_LENGTH, 0, null, null);
024: }
025:
026: public DHParameters(BigInteger p, BigInteger g, BigInteger q, int l) {
027: this (p, g, q, l, l, null, null);
028: }
029:
030: public DHParameters(BigInteger p, BigInteger g, BigInteger q,
031: int m, int l) {
032: this (p, g, q, m, l, null, null);
033: }
034:
035: public DHParameters(BigInteger p, BigInteger g, BigInteger q,
036: BigInteger j, DHValidationParameters validation) {
037: this (p, g, q, DEFAULT_MINIMUM_LENGTH, 0, j, validation);
038: }
039:
040: public DHParameters(BigInteger p, BigInteger g, BigInteger q,
041: int m, int l, BigInteger j,
042: DHValidationParameters validation) {
043: if (l != 0 && m > l) {
044: throw new IllegalArgumentException(
045: "l value must be greater than m value if provided");
046: }
047:
048: this .g = g;
049: this .p = p;
050: this .q = q;
051: this .m = m;
052: this .l = l;
053: this .j = j;
054: this .validation = validation;
055: }
056:
057: public BigInteger getP() {
058: return p;
059: }
060:
061: public BigInteger getG() {
062: return g;
063: }
064:
065: public BigInteger getQ() {
066: return q;
067: }
068:
069: /**
070: * Return the subgroup factor J.
071: *
072: * @return subgroup factor
073: */
074: public BigInteger getJ() {
075: return j;
076: }
077:
078: /**
079: * Return the minimum length of the private value.
080: *
081: * @return the minimum length of the private value in bits.
082: */
083: public int getM() {
084: return m;
085: }
086:
087: /**
088: * Return the private value length in bits - if set, zero otherwise (use bitLength(P) - 1).
089: *
090: * @return the private value length in bits, zero otherwise.
091: */
092: public int getL() {
093: return l;
094: }
095:
096: public DHValidationParameters getValidationParameters() {
097: return validation;
098: }
099:
100: public boolean equals(Object obj) {
101: if (!(obj instanceof DHParameters)) {
102: return false;
103: }
104:
105: DHParameters pm = (DHParameters) obj;
106:
107: if (this .getQ() != null) {
108: if (!this .getQ().equals(pm.getQ())) {
109: return false;
110: }
111: } else {
112: if (pm.getQ() != null) {
113: return false;
114: }
115: }
116:
117: return pm.getP().equals(p) && pm.getG().equals(g);
118: }
119:
120: public int hashCode() {
121: return getP().hashCode() ^ getG().hashCode()
122: ^ (getQ() != null ? getQ().hashCode() : 0);
123: }
124: }
|