Example usage for org.bouncycastle.util Arrays areEqual

List of usage examples for org.bouncycastle.util Arrays areEqual

Introduction

In this page you can find the example usage for org.bouncycastle.util Arrays areEqual.

Prototype

public static boolean areEqual(short[] a, short[] b) 

Source Link

Usage

From source file:org.ejbca.core.protocol.cmp.CrmfRequestMessage.java

License:Open Source License

@Override
public boolean verify() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException {
    boolean ret = false;
    final ProofOfPossession pop = getReq().getPopo();
    if (log.isDebugEnabled()) {
        log.debug("allowRaVerifyPopo: " + allowRaVerifyPopo);
        log.debug("pop.getRaVerified(): " + (pop.getType() == ProofOfPossession.TYPE_RA_VERIFIED));
    }//from ww w .  ja  va  2  s .c  om
    if (allowRaVerifyPopo && (pop.getType() == ProofOfPossession.TYPE_RA_VERIFIED)) {
        ret = true;
    } else if (pop.getType() == ProofOfPossession.TYPE_SIGNING_KEY) {
        try {
            final POPOSigningKey sk = (POPOSigningKey) pop.getObject();
            final POPOSigningKeyInput pski = sk.getPoposkInput();
            ASN1Encodable protObject = pski;
            // Use of POPOSigningKeyInput or not, as described in RFC4211, section 4.1.
            if (pski == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Using CertRequest as POPO input because POPOSigningKeyInput is missing.");
                }
                protObject = getReq().getCertReq();
            } else {
                // Assume POPOSigningKeyInput with the public key and name, MUST be the same as in the request according to RFC4211
                if (log.isDebugEnabled()) {
                    log.debug("Using POPOSigningKeyInput as POPO input.");
                }
                final CertRequest req = getReq().getCertReq();
                // If subject is present in cert template it must be the same as in POPOSigningKeyInput
                final X500Name subject = req.getCertTemplate().getSubject();
                if (subject != null && !subject.toString().equals(pski.getSender().getName().toString())) {
                    log.info("Subject '" + subject.toString() + "', is not equal to '"
                            + pski.getSender().toString() + "'.");
                    protObject = null; // pski is not a valid protection object
                }
                // If public key is present in cert template it must be the same as in POPOSigningKeyInput
                final SubjectPublicKeyInfo pk = req.getCertTemplate().getPublicKey();
                if (pk != null && !Arrays.areEqual(pk.getEncoded(), pski.getPublicKey().getEncoded())) {
                    log.info(
                            "Subject key in cert template, is not equal to subject key in POPOSigningKeyInput.");
                    protObject = null; // pski is not a valid protection object
                }
            }
            // If a protectObject is present we extract the bytes and verify it
            if (protObject != null) {
                final ByteArrayOutputStream bao = new ByteArrayOutputStream();
                new DEROutputStream(bao).writeObject(protObject);
                final byte[] protBytes = bao.toByteArray();
                final AlgorithmIdentifier algId = sk.getAlgorithmIdentifier();
                if (log.isDebugEnabled()) {
                    log.debug(
                            "POP protection bytes length: " + (protBytes != null ? protBytes.length : "null"));
                    log.debug("POP algorithm identifier is: " + algId.getAlgorithm().getId());
                }
                final Signature sig = Signature.getInstance(algId.getAlgorithm().getId(), "BC");
                sig.initVerify(getRequestPublicKey());
                sig.update(protBytes);
                final DERBitString bs = sk.getSignature();
                ret = sig.verify(bs.getBytes());
                if (log.isDebugEnabled()) {
                    log.debug("POP verify returns: " + ret);
                }
            }
        } catch (IOException e) {
            log.error("Error encoding CertReqMsg: ", e);
        } catch (SignatureException e) {
            log.error("SignatureException verifying POP: ", e);
        }
    }
    return ret;
}

From source file:org.ejbca.ui.web.protocol.CrlStoreServletTest.java

License:Open Source License

private void testURI(PrintWriter pw, String sURI, String caSubjectDN, boolean isDelta) throws Exception {
    log.debug("Testing URL: '" + sURI + "'.");
    final HttpURLConnection connection = (HttpURLConnection) new URI(sURI).toURL().openConnection();
    connection.connect();/*from   w  ww.j  a  v  a  2 s.c o m*/
    final int responseCode = connection.getResponseCode();
    if (HttpURLConnection.HTTP_OK != responseCode) {
        pw.println(" Fetching CRL with '" + sURI + "' is not working. responseCode=" + responseCode);
        return;
    }

    final byte fromBean[] = crlSession.getLastCRL(caSubjectDN, isDelta);
    final byte fromURL[] = new byte[connection.getContentLength()];
    connection.getInputStream().read(fromURL);
    if (!Arrays.areEqual(fromBean, fromURL)) {
        pw.println(" CRL from URL and bean are not equal for '" + sURI + "'.");
    }
}

From source file:org.ethereum.core.Block.java

License:Open Source License

private void checkExpectedRoot(byte[] expectedRoot, byte[] calculatedRoot) {
    if (!Arrays.areEqual(expectedRoot, calculatedRoot)) {
        logger.error("Transactions trie root validation failed for block #{}", this.header.getNumber());
        panicProcessor.panic("txroot", String.format("Transactions trie root validation failed for block %d %s",
                this.header.getNumber(), this.header.getHash()));
    }/*from w  ww  . j a  v a2  s .com*/
}

From source file:org.ethereum.util.UtilsTest.java

License:Open Source License

@Test
public void testAddressStringToBytes() {
    // valid address
    String HexStr = "6c386a4b26f73c802f34673f7248bb118f97424a";
    byte[] expected = Hex.decode(HexStr);
    byte[] result = Utils.addressStringToBytes(HexStr);
    assertEquals(Arrays.areEqual(expected, result), true);

    // invalid address, we removed the last char so it cannot decode
    HexStr = "6c386a4b26f73c802f34673f7248bb118f97424";
    expected = null;//from  www.j av a 2 s.  co m
    result = Utils.addressStringToBytes(HexStr);
    assertEquals(expected, result);

    // invalid address, longer than 20 bytes
    HexStr = new String(Hex.encode("I am longer than 20 bytes, i promise".getBytes()));
    expected = null;
    result = Utils.addressStringToBytes(HexStr);
    assertEquals(expected, result);

    // invalid address, shorter than 20 bytes
    HexStr = new String(Hex.encode("I am short".getBytes()));
    expected = null;
    result = Utils.addressStringToBytes(HexStr);
    assertEquals(expected, result);
}

From source file:org.globus.gsi.OpenSSLKey.java

License:Apache License

@Override
public boolean equals(Object other) {
    if (other == this) {
        return true;
    }/*from  w  w  w  .jav a2  s.  c o m*/

    if (!(other instanceof OpenSSLKey)) {
        return false;
    }

    OpenSSLKey otherKey = (OpenSSLKey) other;

    return this.isEncrypted == otherKey.isEncrypted && objectsEquals(this.keyAlg, otherKey.keyAlg)
            && Arrays.areEqual(this.encodedKey, otherKey.encodedKey)
            && objectsEquals(this.intKey, otherKey.intKey) && Arrays.areEqual(this.ivData, otherKey.ivData)
            && objectsEquals(this.encAlgStr, otherKey.encAlgStr) && objectsEquals(this.encAlg, otherKey.encAlg)
            && Arrays.areEqual(this.keyData, otherKey.keyData);
}

From source file:org.hyperledger.account.ShamirsSecretShares.java

License:Apache License

/**
 * Reconstruct a secret from a collection of shares. Provided they are suffcient.
 *
 * @param shares an array of secret shares
 * @return secret if successfully recreated. The algorithm can not check for success if the shares ver not created with verbose serialization.
 * @throws HyperLedgerException/*from   w  w w .j a  v a  2s  .c o m*/
 */
public static PrivateKey reconstruct(String[] shares) throws HyperLedgerException {
    SecretShare ss[] = new SecretShare[shares.length];

    boolean comp = true;
    for (int i = 0; i < shares.length; ++i) {
        byte[] raw = ByteUtils.fromBase58WithChecksum(shares[i]);
        byte[] prefix = Arrays.copyOfRange(raw, 0, 2);
        boolean verbose = Arrays.areEqual(prefix, compressed) || !Arrays.areEqual(prefix, legacy);
        if (!verbose && !Arrays.areEqual(prefix, compressedShort) && !Arrays.areEqual(prefix, legacyShort)) {
            throw new HyperLedgerException("Not a key share");
        }
        ss[i] = new SecretShare();
        ss[i].shareNumber = raw[2] & 0xff;
        ss[i].share = new BigInteger(1, Arrays.copyOfRange(raw, verbose ? 6 : 3, 40));
        comp = raw[1] == compressed[1];
    }
    return new PrivateKey(ss256.reconstruct(ss), comp);
}

From source file:org.hyperledger.common.Address.java

License:Apache License

/**
 * Compare addresses. Note that it is non-standard such that it makes P2Key Address equals LegacyAddress
 *
 * @param obj the other address/*  w w  w.  j  a  v a 2s . c  o m*/
 * @return true if equals
 */
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null || !(obj instanceof Address)) {
        return false;
    }
    return Arrays.areEqual(bytes, ((Address) obj).bytes) && type == ((Address) obj).type;
}

From source file:org.hyperledger.common.MasterPrivateKey.java

License:Apache License

/**
 * Recreate a key from BIP32 serialization
 *
 * @param serialized/* www.ja  va2  s  .c  om*/
 * @return MasterPrivateKey
 * @throws HyperLedgerException
 */
public static MasterPrivateKey parse(String serialized) throws HyperLedgerException {
    byte[] data = ByteUtils.fromBase58WithChecksum(serialized);
    if (data.length != 78) {
        throw new HyperLedgerException("invalid master key");
    }
    byte[] type = Arrays.copyOf(data, 4);
    if (!Arrays.areEqual(type, xprv) && !Arrays.areEqual(type, tprv)) {
        throw new HyperLedgerException("invalid magic number for a master private key");
    }

    int depth = data[4] & 0xff;

    int parent = data[5] & 0xff;
    parent <<= 8;
    parent |= data[6] & 0xff;
    parent <<= 8;
    parent |= data[7] & 0xff;
    parent <<= 8;
    parent |= data[8] & 0xff;

    int sequence = data[9] & 0xff;
    sequence <<= 8;
    sequence |= data[10] & 0xff;
    sequence <<= 8;
    sequence |= data[11] & 0xff;
    sequence <<= 8;
    sequence |= data[12] & 0xff;

    byte[] chainCode = Arrays.copyOfRange(data, 13, 13 + 32);
    byte[] pubOrPriv = Arrays.copyOfRange(data, 13 + 32, data.length);
    return new MasterPrivateKey(new PrivateKey(new BigInteger(1, pubOrPriv), true), chainCode, depth, parent,
            sequence);
}

From source file:org.hyperledger.common.MasterPrivateKey.java

License:Apache License

@Override
public boolean equals(Object obj) {
    if (obj instanceof MasterPrivateKey) {
        return master.equals(((MasterPrivateKey) obj).master)
                && Arrays.areEqual(chainCode, ((MasterPrivateKey) obj).chainCode)
                && depth == ((MasterPrivateKey) obj).depth && parent == ((MasterPrivateKey) obj).parent
                && sequence == ((MasterPrivateKey) obj).sequence;
    }/*ww w  .j  ava  2s . c o  m*/
    return false;
}

From source file:org.hyperledger.common.MasterPublicKey.java

License:Apache License

/**
 * Parse a MasterPublickey from its BIP32 compliant serialization.
 *
 * @param serialized a Base58 string/*  w  ww . j a  v  a 2 s. co m*/
 * @return a master key
 * @throws HyperLedgerException for invalid format
 */
public static MasterPublicKey parse(String serialized) throws HyperLedgerException {
    byte[] data = ByteUtils.fromBase58WithChecksum(serialized);
    if (data.length != 78) {
        throw new HyperLedgerException("invalid extended key");
    }
    byte[] type = Arrays.copyOf(data, 4);
    if (!Arrays.areEqual(type, xpub) && !Arrays.areEqual(type, tpub)) {
        throw new HyperLedgerException("invalid magic number for an master public key");
    }

    int depth = data[4] & 0xff;

    int parent = data[5] & 0xff;
    parent <<= 8;
    parent |= data[6] & 0xff;
    parent <<= 8;
    parent |= data[7] & 0xff;
    parent <<= 8;
    parent |= data[8] & 0xff;

    int sequence = data[9] & 0xff;
    sequence <<= 8;
    sequence |= data[10] & 0xff;
    sequence <<= 8;
    sequence |= data[11] & 0xff;
    sequence <<= 8;
    sequence |= data[12] & 0xff;

    byte[] chainCode = Arrays.copyOfRange(data, 13, 13 + 32);
    byte[] pubOrPriv = Arrays.copyOfRange(data, 13 + 32, data.length);
    return new MasterPublicKey(new PublicKey(pubOrPriv, true), chainCode, depth, parent, sequence);
}