Example usage for java.math BigInteger equals

List of usage examples for java.math BigInteger equals

Introduction

In this page you can find the example usage for java.math BigInteger equals.

Prototype

public boolean equals(Object x) 

Source Link

Document

Compares this BigInteger with the specified Object for equality.

Usage

From source file:Util.java

protected static BigInteger sqrt(BigInteger number, BigInteger guess) {
    // ((n/g) + g)/2: until same result twice in a row
    //      BigInteger result = number.divide(guess).add(guess).divide(BigIntegerTWO);
    //      if(result.compareTo(guess) == 0)
    //         return result;
    ////from   w w  w  .jav a 2 s .  c  o  m
    //      return sqrt(number, result);

    // redoing this to avoid StackOverFlow
    BigInteger result = BigIntegerZERO;
    BigInteger flipA = result;
    BigInteger flipB = result;
    boolean first = true;
    while (result.compareTo(guess) != 0) {
        if (!first)
            guess = result;
        else
            first = false;

        result = number.divide(guess).add(guess).divide(BigIntegerTWO);
        // handle flip flops
        if (result.equals(flipB))
            return flipA;

        flipB = flipA;
        flipA = result;
    }
    return result;

}

From source file:org.eclipse.om2m.binding.http.RestHttpServlet.java

/**
 * Converts a standard HTTP status code into a  {@link StatusCode} object.
 * @param statusCode - protocol-independent status code.
 * @param isEmptyBody - request body existence
 * @return standard HTTP status code./* ww w.j  ava 2 s . c o m*/
 */
public static int getHttpStatusCode(BigInteger statusCode) {
    if (statusCode.equals(ResponseStatusCode.OK) || statusCode.equals(ResponseStatusCode.UPDATED)
            || statusCode.equals(ResponseStatusCode.DELETED)) {
        return 200;
    } else if (statusCode.equals(ResponseStatusCode.CREATED)) {
        return 201;
    } else if (statusCode.equals(ResponseStatusCode.ACCEPTED)) {
        return 202;
    } else if (statusCode.equals(ResponseStatusCode.BAD_REQUEST)
            || statusCode.equals(ResponseStatusCode.CONTENTS_UNACCEPTABLE)
            || statusCode.equals(ResponseStatusCode.MAX_NUMBER_OF_MEMBER_EXCEEDED)
            || statusCode.equals(ResponseStatusCode.MEMBER_TYPE_INCONSISTENT)
            || statusCode.equals(ResponseStatusCode.INVALID_CMDTYPE)
            || statusCode.equals(ResponseStatusCode.INSUFFICIENT_ARGUMENTS)
            || statusCode.equals(ResponseStatusCode.ALREADY_COMPLETED)
            || statusCode.equals(ResponseStatusCode.COMMAND_NOT_CANCELLABLE)) {
        return 400;
    } else if (statusCode.equals(ResponseStatusCode.ACCESS_DENIED)
            || statusCode.equals(ResponseStatusCode.SUBSCRIPTION_CREATOR_HAS_NO_PRIVILEGE)
            || statusCode.equals(ResponseStatusCode.NO_PRIVILEGE)
            || statusCode.equals(ResponseStatusCode.ALREADY_EXISTS)
            || statusCode.equals(ResponseStatusCode.TARGET_NOT_SUBSCRIBABLE)
            || statusCode.equals(ResponseStatusCode.SUBSCRIPTION_HOST_HAS_NO_PRIVILEGE)) {
        return 403;
    } else if (statusCode.equals(ResponseStatusCode.NOT_FOUND)
            || statusCode.equals(ResponseStatusCode.TARGET_NOT_REACHABLE)
            || statusCode.equals(ResponseStatusCode.EXTERNAL_OBJECT_NOT_FOUND)
            || statusCode.equals(ResponseStatusCode.EXTERNAL_OBJECT_NOT_REACHABLE)) {
        return 404;
    } else if (statusCode.equals(ResponseStatusCode.OPERATION_NOT_ALLOWED)) {
        return 405;
    } else if (statusCode.equals(ResponseStatusCode.REQUEST_TIMEOUT)) {
        return 408;
    } else if (statusCode.equals(ResponseStatusCode.CONFLICT)
            || statusCode.equals(ResponseStatusCode.GROUP_REQUEST_IDENTIFIER_EXISTS)) {
        return 409;
    } else if (statusCode.equals(ResponseStatusCode.INTERNAL_SERVER_ERROR)
            || statusCode.equals(ResponseStatusCode.SUBSCRIPTION_VERIFICATION_INITIATION_FAILED)
            || statusCode.equals(ResponseStatusCode.MGMT_SESSION_CANNOT_BE_ESTABLISHED)
            || statusCode.equals(ResponseStatusCode.MGMT_SESSION_ESTABLISHMENT_TIMEOUT)
            || statusCode.equals(ResponseStatusCode.MGMT_CONVERSION_ERROR)
            || statusCode.equals(ResponseStatusCode.MGMT_CANCELATION_FAILURE)) {
        return 500;
    } else if (statusCode.equals(ResponseStatusCode.NOT_IMPLEMENTED)
            || statusCode.equals(ResponseStatusCode.NON_BLOCKING_REQUEST_NOT_SUPPORTED)) {
        return 501;
    } else if (statusCode.equals(ResponseStatusCode.SERVICE_UNAVAILABLE)) {
        return 503;
    }
    return 501;
}

From source file:org.apache.jmeter.assertions.SMIMEAssertion.java

private static AssertionResult verifySignature(SMIMEAssertionTestElement testElement, SMIMESignedParser s,
        String name) throws CMSException {
    AssertionResult res = new AssertionResult(name);

    try {/*from w w  w  . java  2  s  .  c o m*/
        Store certs = s.getCertificates();
        SignerInformationStore signers = s.getSignerInfos();
        Iterator<?> signerIt = signers.getSigners().iterator();

        if (signerIt.hasNext()) {

            SignerInformation signer = (SignerInformation) signerIt.next();
            Iterator<?> certIt = certs.getMatches(signer.getSID()).iterator();

            if (certIt.hasNext()) {
                // the signer certificate
                X509CertificateHolder cert = (X509CertificateHolder) certIt.next();

                if (testElement.isVerifySignature()) {

                    SignerInformationVerifier verifier = null;
                    try {
                        verifier = new JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(cert);
                    } catch (OperatorCreationException e) {
                        log.error("Can't create a provider", e);
                    }
                    if (verifier == null || !signer.verify(verifier)) {
                        res.setFailure(true);
                        res.setFailureMessage("Signature is invalid");
                    }
                }

                if (testElement.isSignerCheckConstraints()) {
                    StringBuilder failureMessage = new StringBuilder();

                    String serial = testElement.getSignerSerial();
                    if (!JOrphanUtils.isBlank(serial)) {
                        BigInteger serialNbr = readSerialNumber(serial);
                        if (!serialNbr.equals(cert.getSerialNumber())) {
                            res.setFailure(true);
                            failureMessage.append("Serial number ").append(serialNbr)
                                    .append(" does not match serial from signer certificate: ")
                                    .append(cert.getSerialNumber()).append("\n");
                        }
                    }

                    String email = testElement.getSignerEmail();
                    if (!JOrphanUtils.isBlank(email)) {
                        List<String> emailFromCert = getEmailFromCert(cert);
                        if (!emailFromCert.contains(email)) {
                            res.setFailure(true);
                            failureMessage.append("Email address \"").append(email)
                                    .append("\" not present in signer certificate\n");
                        }

                    }

                    String subject = testElement.getSignerDn();
                    if (subject.length() > 0) {
                        final X500Name certPrincipal = cert.getSubject();
                        log.debug("DN from cert: " + certPrincipal.toString());
                        X500Name principal = new X500Name(subject);
                        log.debug("DN from assertion: " + principal.toString());
                        if (!principal.equals(certPrincipal)) {
                            res.setFailure(true);
                            failureMessage.append("Distinguished name of signer certificate does not match \"")
                                    .append(subject).append("\"\n");
                        }
                    }

                    String issuer = testElement.getIssuerDn();
                    if (issuer.length() > 0) {
                        final X500Name issuerX500Name = cert.getIssuer();
                        log.debug("IssuerDN from cert: " + issuerX500Name.toString());
                        X500Name principal = new X500Name(issuer);
                        log.debug("IssuerDN from assertion: " + principal);
                        if (!principal.equals(issuerX500Name)) {
                            res.setFailure(true);
                            failureMessage
                                    .append("Issuer distinguished name of signer certificate does not match \"")
                                    .append(subject).append("\"\n");
                        }
                    }

                    if (failureMessage.length() > 0) {
                        res.setFailureMessage(failureMessage.toString());
                    }
                }

                if (testElement.isSignerCheckByFile()) {
                    CertificateFactory cf = CertificateFactory.getInstance("X.509");
                    X509CertificateHolder certFromFile;
                    InputStream inStream = null;
                    try {
                        inStream = new BufferedInputStream(
                                new FileInputStream(testElement.getSignerCertFile()));
                        certFromFile = new JcaX509CertificateHolder(
                                (X509Certificate) cf.generateCertificate(inStream));
                    } finally {
                        IOUtils.closeQuietly(inStream);
                    }

                    if (!certFromFile.equals(cert)) {
                        res.setFailure(true);
                        res.setFailureMessage("Signer certificate does not match certificate "
                                + testElement.getSignerCertFile());
                    }
                }

            } else {
                res.setFailure(true);
                res.setFailureMessage("No signer certificate found in signature");
            }

        }

        // TODO support multiple signers
        if (signerIt.hasNext()) {
            log.warn("SMIME message contains multiple signers! Checking multiple signers is not supported.");
        }

    } catch (GeneralSecurityException e) {
        log.error(e.getMessage(), e);
        res.setError(true);
        res.setFailureMessage(e.getMessage());
    } catch (FileNotFoundException e) {
        res.setFailure(true);
        res.setFailureMessage("certificate file not found: " + e.getMessage());
    }

    return res;
}

From source file:spade.utility.CommonFunctions.java

public static boolean bigIntegerEquals(BigInteger a, BigInteger b) {
    if (a == null && b == null) {
        return true;
    } else if ((a == null && b != null) || (a != null && b == null)) {
        return false;
    } else {/*from   www.j  a  va2s .c o m*/
        return a.equals(b);
    }
}

From source file:com.aaasec.sigserv.csspserver.SpServlet.java

private static SpSession getSessionFromID(BigInteger sessionID) {
    SpSession session = null;//  ww w. ja  v a2 s.co  m
    List<BigInteger> removeList = new ArrayList<BigInteger>();
    Set<BigInteger> idSet = sessionMap.keySet();
    for (BigInteger id : idSet) {
        SpSession storedSession = sessionMap.get(id);
        long lastUse = storedSession.getLastUsed();
        if ((lastUse + day) < System.currentTimeMillis()) {
            removeList.add(id);
            continue;
        }
        if (id.equals(sessionID)) {
            storedSession.setLastUsed(System.currentTimeMillis());
            session = storedSession;
        }
    }
    //cleanup
    for (BigInteger id : removeList) {
        if (sessionMap.containsKey(id)) {
            SpSession deleteSession = sessionMap.get(id);
            deleteSession.clear();
            sessionMap.remove(id);
        }
    }

    if (session == null) {
        session = new SpSession(sessionID, FileOps.getfileNameString(SpModel.getDataDir(), "sigTemp"));
        session.setLastUsed(System.currentTimeMillis());
        sessionMap.put(sessionID, session);
    }
    return session;
}

From source file:net.spfbl.whois.SubnetIPv6.java

public static String getNextIPv6(String ip) {
    if (ip == null) {
        return null;
    } else if (SubnetIPv6.isValidIPv6(ip)) {
        BigInteger address = new BigInteger(1, address(ip));
        if (address.equals(ADDRESS_MAX)) {
            return null;
        } else {/*from w  w w  . j av a2  s .c  o m*/
            address = address.add(ADDRESS_UNIT);
            int p8 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p7 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p6 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p5 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p4 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p3 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p2 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p1 = address.and(ADDRESS_OCTET).intValue();
            return Integer.toHexString(p1) + ":" + Integer.toHexString(p2) + ":" + Integer.toHexString(p3) + ":"
                    + Integer.toHexString(p4) + ":" + Integer.toHexString(p5) + ":" + Integer.toHexString(p6)
                    + ":" + Integer.toHexString(p7) + ":" + Integer.toHexString(p8);
        }
    } else {
        return null;
    }
}

From source file:net.spfbl.whois.SubnetIPv6.java

public static String getPreviousIPv6(String ip) {
    if (ip == null) {
        return null;
    } else if (SubnetIPv6.isValidIPv6(ip)) {
        BigInteger address = new BigInteger(1, address(ip));
        if (address.equals(ADDRESS_MIN)) {
            return null;
        } else {/*from   ww  w. j  a  v  a 2  s. co m*/
            address = address.subtract(ADDRESS_UNIT);
            int p8 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p7 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p6 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p5 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p4 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p3 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p2 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p1 = address.and(ADDRESS_OCTET).intValue();
            return Integer.toHexString(p1) + ":" + Integer.toHexString(p2) + ":" + Integer.toHexString(p3) + ":"
                    + Integer.toHexString(p4) + ":" + Integer.toHexString(p5) + ":" + Integer.toHexString(p6)
                    + ":" + Integer.toHexString(p7) + ":" + Integer.toHexString(p8);
        }
    } else {
        return null;
    }
}

From source file:eu.europa.esig.dss.DSSRevocationUtils.java

/**
 * fix for certId.equals methods that doesn't work very well.
 *
 * @param certId//from  ww  w  . j  a v  a 2s.  c  o  m
 *            {@code CertificateID}
 * @param singleResp
 *            {@code SingleResp}
 * @return true if the certificate matches this included in
 *         {@code SingleResp}
 */
public static boolean matches(final CertificateID certId, final SingleResp singleResp) {

    final CertificateID singleRespCertID = singleResp.getCertID();
    final ASN1ObjectIdentifier singleRespCertIDHashAlgOID = singleRespCertID.getHashAlgOID();
    final byte[] singleRespCertIDIssuerKeyHash = singleRespCertID.getIssuerKeyHash();
    final byte[] singleRespCertIDIssuerNameHash = singleRespCertID.getIssuerNameHash();
    final BigInteger singleRespCertIDSerialNumber = singleRespCertID.getSerialNumber();

    final ASN1ObjectIdentifier certIdHashAlgOID = certId.getHashAlgOID();
    final byte[] certIdIssuerKeyHash = certId.getIssuerKeyHash();
    final byte[] certIdIssuerNameHash = certId.getIssuerNameHash();
    final BigInteger certIdSerialNumber = certId.getSerialNumber();

    // certId.equals fails in comparing the algoIdentifier because
    // AlgoIdentifier params in null in one case and DERNull in another case
    return singleRespCertIDHashAlgOID.equals(certIdHashAlgOID)
            && Arrays.areEqual(singleRespCertIDIssuerKeyHash, certIdIssuerKeyHash)
            && Arrays.areEqual(singleRespCertIDIssuerNameHash, certIdIssuerNameHash)
            && singleRespCertIDSerialNumber.equals(certIdSerialNumber);
}

From source file:org.opendaylight.vpnservice.itm.impl.ItmUtils.java

public static List<DPNTEPsInfo> getDPNTEPListFromDPNId(DataBroker dataBroker, List<BigInteger> dpnIds) {
    List<DPNTEPsInfo> meshedDpnList = getTunnelMeshInfo(dataBroker);
    List<DPNTEPsInfo> cfgDpnList = new ArrayList<DPNTEPsInfo>();
    if (null != meshedDpnList) {
        for (BigInteger dpnId : dpnIds) {
            for (DPNTEPsInfo teps : meshedDpnList) {
                if (dpnId.equals(teps.getDPNID()))
                    cfgDpnList.add(teps);
            }//from w ww . j  a va  2  s  .c o m
        }
    }
    return cfgDpnList;
}

From source file:org.stem.domain.DHT.java

public int getSection(byte[] keyBytes) {
    BigInteger token = new BigInteger(new String(Hex.encodeHex(keyBytes)), 16);
    BigInteger sectionSize = capacity.divide(BigInteger.valueOf(sections));

    int quotient = token.divide(sectionSize).intValue();
    BigInteger mod = token.mod(sectionSize);
    int index;//www . ja va  2 s.co  m
    if (quotient == 0)
        index = 0;
    else if (mod.equals(BigInteger.ZERO))
        index = Math.max(quotient - 1, 0);
    else
        index = Math.max(quotient, 0);

    if (index >= sections) {
        return sections - 1;
    }

    return index;
}