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:ie.peternagy.jcrypto.algo.AesWrapperTest.java

License:Open Source License

/**
 * Test doFinalWithHeaders - compact init
 *//*w  w w .j av a  2s .  co  m*/
@org.junit.Test
public void testDoFinalWithHeadersCompact() {
    System.out.println("doFinalWithHeaders - compact");
    input = CryptoSecurityUtil.getSecureBytes(128);
    aesWrapper = new AesWrapper(new EllipticCurveWrapper(), true);

    byte[] data = aesWrapper.doFinalWithHeader(input);
    aesWrapper = new AesWrapper(new EllipticCurveWrapper(), false);
    byte[] result = aesWrapper.doFinalWithHeader(data);

    assertTrue(Arrays.areEqual(result, input));
}

From source file:ie.peternagy.jcrypto.algo.EllipticCurveWrapperTest.java

License:Open Source License

/**
 * Test of doFinal method, of class EllipticCurveWrapper.
 *///from   w w  w .ja v  a 2 s. c om
@Test
public void testDoFinal() {
    System.out.println("doFinal - encrypt and decrypt");
    curve.tryLoadKeys();
    byte[] encData = curve.doFinal(input, true);
    byte[] decData = curve.doFinal(encData, false);

    assertTrue(Arrays.areEqual(decData, input));
}

From source file:ie.peternagy.jcrypto.algo.EllipticCurveWrapperTest.java

License:Open Source License

/**
 * Test of doFinalWithHeader method, of class EllipticCurveWrapper.
 *///from   w  ww  .j a v a2  s  .co  m
@Test
public void testDoFinalWithHeader() {
    System.out.println("doFinalWithHeader");
    curve.tryLoadKeys();
    byte[] encData = curve.doFinalWithHeader(input, true);
    byte[] decData = curve.doFinalWithHeader(encData, false);

    assertTrue(Arrays.areEqual(decData, input));
}

From source file:ie.peternagy.jcrypto.algo.EllipticCurveWrapperTest.java

License:Open Source License

/**
 * Test of getKeyId method, of class EllipticCurveWrapper.
 *///from   w  ww .  jav  a  2  s  . c o  m
@Test
public void testGetKeyId() {
    System.out.println("getKeyId");
    curve.tryLoadKeys();
    byte[] expResult = curve.getKeyId();
    byte[] result = curve.getKeyId();

    assertTrue(Arrays.areEqual(expResult, result));
}

From source file:kpl.crypto.Curve25519Test.java

@Test
public void testAlterSignature() {
    byte[] sig = new byte[32];
    byte[] h = new byte[32];
    byte[] signPriv = new byte[32];
    byte[] pub = new byte[32];
    byte[] secret = Convert.parseHexString(ALICE_SECRET);
    Curve25519.keygen(pub, signPriv, secret);
    new SecureRandom().nextBytes(h);
    Curve25519.sign(sig, h, secret, signPriv);
    sig[0] += 1;/* w w w.  ja  va 2 s  .  com*/
    byte[] v = new byte[32];
    Curve25519.verify(v, sig, h, pub);

    if (Arrays.areEqual(v, pub)) {
        fail("Should not verify, as signature is altered");
    }
}

From source file:net.floodlightcontroller.flowcache.FlowCacheDB.java

License:Open Source License

@Override
public synchronized Map<Long, Set<FlowCacheObj>> queryDB(long switchId, FlowCacheQuery query) {
    /* New HashMap that contains all flow cache objects that match the query: switchId -> SetOf FlowCacheObj. */
    HashMap<Long, Set<FlowCacheObj>> resultMap = new HashMap<Long, Set<FlowCacheObj>>();

    if (!this.flowCacheMatchHashMap.containsKey(switchId)) {
        if (log.isDebugEnabled()) {
            log.debug("Switch ID {} not found.", HexString.toHexString(switchId));
        }/*from w  w  w. j a va 2s.  c om*/
        return null;
    }

    // Add all flow cache objects of the switch to the resulting map.
    if (!resultMap.containsKey(switchId)) {
        resultMap.put(switchId, new HashSet<FlowCacheObj>());
    }
    resultMap.get(switchId).addAll(this.flowCacheMatchHashMap.get(switchId).values());

    // Remove unwanted entries - according to the flow cache query.
    for (Iterator<FlowCacheObj> iter = resultMap.get(switchId).iterator(); iter.hasNext();) {
        // Get the flow cache object.
        FlowCacheObj fco = iter.next();

        // Cookie
        if (query.cookie != 0 && query.cookie != fco.getCookie()) {
            iter.remove();
            continue;
        }
        // priority
        if (query.priority != 0 && query.priority != fco.getPriority()) {
            iter.remove();
            continue;
        }
        // OutPort
        if (query.outPort != 0 && (fco.getOutPorts() == null || !fco.getOutPorts().contains(query.outPort))) {
            iter.remove();
            continue;
        }
        // InPort
        if (query.inPort != 0 && (fco.getMatch() == null || query.inPort != fco.getMatch().getInputPort())) {
            iter.remove();
            continue;
        }
        // DL_DST
        if (query.dataLayerDestination != null
                && !Arrays.areEqual(query.dataLayerDestination, fco.getMatch().getDataLayerDestination())) {
            iter.remove();
            continue;
        }
        // DL_SRC
        if (query.dataLayerSource != null
                && !Arrays.areEqual(query.dataLayerSource, fco.getMatch().getDataLayerSource())) {
            iter.remove();
            continue;
        }
        // DL_VLAN
        if (query.dataLayerVirtualLan != 0
                && query.dataLayerVirtualLan != fco.getMatch().getDataLayerVirtualLan()) {
            iter.remove();
            continue;
        }
        // DL_VLAN_PCP
        if (query.dataLayerVirtualLanPriorityCodePoint != 0 && query.dataLayerVirtualLanPriorityCodePoint != fco
                .getMatch().getDataLayerVirtualLanPriorityCodePoint()) {
            iter.remove();
            continue;
        }
        // DL_TYPE
        if (query.dataLayerType != 0 && query.dataLayerType != fco.getMatch().getDataLayerType()) {
            iter.remove();
            continue;
        }
        // NW_PROTO
        if (query.networkProtocol != 0 && query.networkProtocol != fco.getMatch().getNetworkProtocol()) {
            iter.remove();
            continue;
        }
        // NW_TOS
        if (query.networkTypeOfService != 0
                && query.networkTypeOfService != fco.getMatch().getNetworkTypeOfService()) {
            iter.remove();
            continue;
        }
        // NW_SRC
        if (query.networkSource != 0 && query.networkSource != fco.getMatch().getNetworkSource()) {
            iter.remove();
            continue;
        }
        // NW_DST
        if (query.networkDestination != 0
                && query.networkDestination != fco.getMatch().getNetworkDestination()) {
            iter.remove();
            continue;
        }
        // TP_SRC
        if (query.transportSource != 0 && query.transportSource != fco.getMatch().getTransportSource()) {
            iter.remove();
            continue;
        }
        // TP_DST
        if (query.transportDestination != 0
                && query.transportDestination != fco.getMatch().getTransportDestination()) {
            iter.remove();
            continue;
        }

        // Path ID
        if (query.pathId > 0 && query.pathId != fco.getPathId()) {
            iter.remove();
            continue;
        }
    }

    //      // TESTING AND DEBUGGING
    //      System.out.println("--------------------------");
    //      for (long swId : this.flowCacheMatchHashMap.keySet()) {
    //         System.out.println("SwitchID: " + HexString.toHexString(swId));
    //         for (int hash : this.flowCacheMatchHashMap.get(swId).keySet()) {
    //            System.out.println("   " + hash + ": " + this.flowCacheMatchHashMap.get(swId).get(hash));
    //         }
    //      }

    // Remove empty SetOf FlowCacheObj and corresponding switch IDs.
    for (long swId : resultMap.keySet()) {
        if (resultMap.get(swId) == null || resultMap.get(swId).isEmpty()) {
            resultMap.remove(swId);
        }
    }

    // Return new result map, or null if result map is empty.
    return (!resultMap.isEmpty()) ? resultMap : null;
}

From source file:net.nharyes.secrete.actions.GenKeysAction.java

License:Open Source License

@Override
public void execute(CommandLine line, SecureRandom random) throws ActionException {

    try {/*ww w  . ja va 2s .co  m*/

        // generate keys
        KeyPair keyPair = Curve25519KeyPairGenerator.generateKeyPair(random);

        // store public key
        FileOutputStream fout = new FileOutputStream(DEFAULT_PUBLIC_KEY);
        Curve25519PublicKey pkey = (Curve25519PublicKey) keyPair.getPublic();
        pkey.serialize(fout);
        fout.flush();
        fout.close();

        // get console
        Console c = getConsole();

        // ask password
        char[] password = c.readPassword("Enter password: ");
        char[] passwordRepeated = c.readPassword("Enter again: ");

        // check password
        if (!Arrays.areEqual(password, passwordRepeated)) {

            System.err.println("The password doesn't match.");
            System.exit(-1);
        }

        // store private key
        fout = new FileOutputStream(DEFAULT_PRIVATE_KEY);
        Curve25519PrivateKey key = (Curve25519PrivateKey) keyPair.getPrivate();
        key.serialize(fout, passwordRepeated);
        fout.flush();
        fout.close();

    } catch (IOException ex) {

        // re-throw exception
        throw new ActionException(ex.getMessage(), ex);
    }
}

From source file:net.nharyes.secrete.curve.Curve25519PrivateKey.java

License:Open Source License

public static Curve25519PrivateKey deserialize(InputStream in, char[] password) throws IOException {

    try {/*from  w w  w . j av a2s. co m*/

        // check magic number
        byte[] mn = new byte[MagicNumbers.PRIVATE_KEY.length];
        IOUtils.readFully(in, mn, 0, mn.length);
        if (!Arrays.areEqual(mn, MagicNumbers.PRIVATE_KEY))
            throw new IllegalArgumentException("Wrong key file format");

        // read initial vector
        byte[] iv = new byte[16];
        IOUtils.readFully(in, iv, 0, iv.length);

        // read salt
        byte[] salt = new byte[64];
        IOUtils.readFully(in, salt, 0, salt.length);

        // initialize cipher
        CipherParameters params = new ParametersWithIV(new KeyParameter(deriveKey(password, salt)), iv);
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
                new PKCS7Padding());
        cipher.reset();
        cipher.init(false, params);

        // decrypt key
        CipherInputStream cin = new CipherInputStream(in, cipher);
        byte[] key = new byte[Curve25519.KEY_SIZE];
        IOUtils.readFully(cin, key, 0, key.length);

        // return key instance
        return new Curve25519PrivateKey(key);

    } catch (UnsupportedEncodingException ex) {

        throw new UnsupportedOperationException(ex.getMessage(), ex);
    }
}

From source file:net.nharyes.secrete.curve.Curve25519PublicKey.java

License:Open Source License

public static Curve25519PublicKey deserialize(InputStream in) throws IOException {

    // check magic number
    byte[] mn = new byte[MagicNumbers.PUBLIC_KEY.length];
    IOUtils.readFully(in, mn, 0, mn.length);
    if (!Arrays.areEqual(mn, MagicNumbers.PUBLIC_KEY))
        throw new IllegalArgumentException("Wrong key file format");

    // read key//from w ww . j  ava 2 s .c  om
    byte[] key = new byte[Curve25519.KEY_SIZE];
    IOUtils.readFully(in, key, 0, key.length);

    // return key instance
    return new Curve25519PublicKey(key);
}

From source file:net.nharyes.secrete.ecies.ECIESMessage.java

License:Open Source License

public static ECIESMessage deserialize(InputStream in) throws IOException {

    // check magic number
    boolean binary;
    byte[] mn = new byte[MagicNumbers.TEXT_MESSAGE.length];
    IOUtils.readFully(in, mn, 0, mn.length);
    if (Arrays.areEqual(mn, MagicNumbers.TEXT_MESSAGE))
        binary = false;/*w  w  w. j  a  va2  s . com*/
    else if (Arrays.areEqual(mn, MagicNumbers.BINARY_MESSAGE))
        binary = true;
    else
        throw new IllegalArgumentException("Wrong file format");

    // read message components
    byte[] sh1 = new byte[ECIES.SHARED_INFORMATION_SIZE_BYTES];
    IOUtils.readFully(in, sh1, 0, sh1.length);
    byte[] sh2 = new byte[ECIES.SHARED_INFORMATION_SIZE_BYTES];
    IOUtils.readFully(in, sh2, 0, sh2.length);
    byte[] iv = new byte[ECIES.IV_SIZE_BYTES];
    IOUtils.readFully(in, iv, 0, iv.length);
    byte[] R = new byte[Curve25519.KEY_SIZE];
    IOUtils.readFully(in, R, 0, R.length);

    // read CD size
    byte[] cdSizeB = new byte[4];
    IOUtils.readFully(in, cdSizeB, 0, cdSizeB.length);
    int cdSize = ByteBuffer.wrap(cdSizeB).getInt();

    // read CD
    byte[] cd = new byte[cdSize];
    IOUtils.readFully(in, cd, 0, cd.length);

    return new ECIESMessage(sh1, sh2, iv, R, cd, binary);
}