org.bouncycastle.crypto.params.DHPublicKeyParameters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk15on Show documentation
Show all versions of bcprov-jdk15on Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 and up.
package org.bouncycastle.crypto.params;
import java.math.BigInteger;
public class DHPublicKeyParameters
extends DHKeyParameters
{
private static final BigInteger ONE = BigInteger.valueOf(1);
private static final BigInteger TWO = BigInteger.valueOf(2);
private BigInteger y;
public DHPublicKeyParameters(
BigInteger y,
DHParameters params)
{
super(false, params);
this.y = validate(y, params);
}
private BigInteger validate(BigInteger y, DHParameters dhParams)
{
if (y == null)
{
throw new NullPointerException("y value cannot be null");
}
// TLS check
if (y.compareTo(TWO) < 0 || y.compareTo(dhParams.getP().subtract(TWO)) > 0)
{
throw new IllegalArgumentException("invalid DH public key");
}
if (dhParams.getQ() != null)
{
if (ONE.equals(y.modPow(dhParams.getQ(), dhParams.getP())))
{
return y;
}
throw new IllegalArgumentException("Y value does not appear to be in correct group");
}
else
{
return y; // we can't validate without Q.
}
}
public BigInteger getY()
{
return y;
}
public int hashCode()
{
return y.hashCode() ^ super.hashCode();
}
public boolean equals(
Object obj)
{
if (!(obj instanceof DHPublicKeyParameters))
{
return false;
}
DHPublicKeyParameters other = (DHPublicKeyParameters)obj;
return other.getY().equals(y) && super.equals(obj);
}
}