~ /code /validator
Code · Validation

Validator.java

Centralize the mathematical and input validation rules required by the key-exchange flow.

24 linesisProbablePrime(100)1 < α < q

Role in the system#

Three pure functions. isPrime runs a probabilistic primality test, isAlphaInRange enforces 1 < α < q, and isValidMessage requires more than 20 characters.

Source#

Validator.java
import java.math.BigInteger;

/**
 * Minimal Validator class for validating prime numbers, alpha values,
 * and message lengths used in the Diffie-Hellman key exchange system.
 */
public class Validator {

    public static boolean isPrime(BigInteger q) {
        if (q == null) return false;
        if (q.compareTo(BigInteger.valueOf(3)) < 0) return false;
        return q.isProbablePrime(100);
    }

    public static boolean isAlphaInRange(BigInteger alpha, BigInteger q) {
        return alpha != null && q != null
                && alpha.compareTo(BigInteger.ONE) > 0
                && alpha.compareTo(q) < 0;
    }

    public static boolean isValidMessage(String message) {
        return message != null && message.trim().length() > 20;
    }
}