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:com.licel.jcardsim.crypto.KeyAgreementImplTest.java

License:Apache License

/**
 * Base method generateSecret//from  w w  w  .j a  v  a  2  s . c om
 * @param keyAlg - key generation algorithm
 * @param keySize - key size
 * @param keyAgreementAlg - key agreement algorithm
 */
public void testGenerateSecret(byte keyAlg, short keySize, byte keyAgreementAlg) {
    // generate keys
    KeyPair kp = new KeyPair(keyAlg, keySize);
    kp.genKeyPair();
    PrivateKey privateKey1 = kp.getPrivate();
    ECPublicKey publicKey1 = (ECPublicKey) kp.getPublic();
    kp.genKeyPair();
    PrivateKey privateKey2 = kp.getPrivate();
    ECPublicKey publicKey2 = (ECPublicKey) kp.getPublic();
    // generate first secret
    KeyAgreement ka = KeyAgreement.getInstance(keyAgreementAlg, false);
    byte[] secret1 = new byte[20];
    byte[] public2 = new byte[128];
    short publicKeyLength = publicKey2.getW(public2, (short) 0);
    ka.init(privateKey1);
    short secret1Size = ka.generateSecret(public2, (short) 0, publicKeyLength, secret1, (short) 0);
    // generate second secret
    byte[] secret2 = new byte[20];
    byte[] public1 = new byte[128];
    publicKeyLength = publicKey1.getW(public1, (short) 0);
    ka.init(privateKey2);
    short secret2Size = ka.generateSecret(public1, (short) 0, publicKeyLength, secret2, (short) 0);
    // sha1 size = 20
    assertEquals(secret1Size, 20);
    assertEquals(secret2Size, 20);
    assertEquals(true, Arrays.areEqual(secret1, secret2));
}

From source file:com.licel.jcardsim.crypto.MessageDigestImplTest.java

License:Apache License

/**
 * Test method <code>doFinal</code>
 * @param engine tested engine//from  ww w  . j av  a 2 s  . com
 * @param msg byte array contains etalon message
 * @param etalonDigest byte array contains etalon digest
 */
public void testEngineDoFinal(MessageDigest engine, byte[] msg, byte[] etalonDigest) {
    byte[] digest = new byte[engine.getLength()];
    engine.doFinal(msg, (short) 0, (short) msg.length, digest, (short) 0);
    assertEquals(true, Arrays.areEqual(etalonDigest, digest));
}

From source file:com.licel.jcardsim.crypto.MessageDigestImplTest.java

License:Apache License

/**
 * Test sequence method's calls <code>doUpdate();doFinal()</code>
 * @param engine tested engine// ww w  .  j  av a2 s .  co m
 * @param msg byte array contains etalon message
 * @param etalonDigest byte array contains etalon digest
 */
public void testEngineDoUpdateFinal(MessageDigest engine, byte[] msg, byte[] etalonDigest) {
    byte[] digest = new byte[engine.getLength()];
    engine.update(msg, (short) 0, (short) 7);
    engine.doFinal(msg, (short) 7, (short) (msg.length - 7), digest, (short) 0);
    assertEquals(true, Arrays.areEqual(etalonDigest, digest));
}

From source file:com.licel.jcardsim.crypto.MessageDigestImplTest.java

License:Apache License

/**
 * Test of setInitialDigest method, of class MessageDigestImpl.
 */// w ww.  j  av  a 2  s.co  m
public void testSetInitialDigest() {
    byte[] initialDigestBuf = new byte[128];
    byte[] inputData = new byte[254];
    rnd.nextBytes(inputData);

    MessageDigestImpl[] digests = new MessageDigestImpl[] { engineSHA1, engineMD5, engineRIPEMD160,
            engineSHA256, engineSHA384, engineSHA512 };

    for (short i = 0; i < digests.length; i++) {
        System.out.println("testSetInitialDigest() - " + digests[i].getAlgorithm());
        byte[] digest = new byte[digests[i].getLength()];
        byte[] etalonDigest = new byte[digests[i].getLength()];
        // calc first part
        short part = digests[i].getBlockSize();
        digests[i].update(inputData, (short) 0, part);
        short initialDigestOff = (short) rnd.nextInt(initialDigestBuf.length - digests[i].getLength());
        digests[i].getIntermediateDigest(initialDigestBuf, initialDigestOff);
        // doFinal
        digests[i].doFinal(inputData, part, (short) (inputData.length - part), digest, (short) 0);
        // etalon
        digests[i].reset();
        digests[i].update(inputData, (short) 0, part);
        digests[i].doFinal(inputData, part, (short) (inputData.length - part), etalonDigest, (short) 0);
        assertEquals(true, Arrays.areEqual(etalonDigest, digest));
    }
}

From source file:com.licel.jcardsim.crypto.RSACipherTest.java

License:Apache License

/**
 * SelfTest of RSA Encryption/Decryption, of class AsymmetricCipherImpl and ByteContainer.getBigInteger() method.
 */// www.j av a2 s.  c  o  m
public void testSelfRSA() {
    Cipher cipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);

    RSAPrivateKey privateKey = (RSAPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE,
            KeyBuilder.LENGTH_RSA_512, false);
    RSAPublicKey publicKey = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC,
            KeyBuilder.LENGTH_RSA_512, false);

    privateKey.setExponent(rsaPrivateKeyExponent, (short) 0, (short) rsaPrivateKeyExponent.length);
    privateKey.setModulus(rsaPrivateKeyModulus, (short) 0, (short) rsaPrivateKeyModulus.length);
    publicKey.setExponent(rsaPublicKeyExponent, (short) 0, (short) rsaPublicKeyExponent.length);
    publicKey.setModulus(rsaPrivateKeyModulus, (short) 0, (short) rsaPrivateKeyModulus.length);

    cipher.init(publicKey, Cipher.MODE_ENCRYPT);
    byte[] msg = new byte[63];
    byte[] encryptedMsg = new byte[64];
    RandomData rnd = RandomData.getInstance(RandomData.ALG_PSEUDO_RANDOM);
    rnd.generateData(msg, (short) 0, (short) msg.length);
    cipher.doFinal(msg, (short) 0, (short) msg.length, encryptedMsg, (short) 0);

    cipher.init(privateKey, Cipher.MODE_DECRYPT);
    byte[] decryptedMsg = new byte[msg.length];
    cipher.doFinal(encryptedMsg, (short) 0, (short) encryptedMsg.length, decryptedMsg, (short) 0);

    assertEquals(true, Arrays.areEqual(msg, decryptedMsg));
}

From source file:com.licel.jcardsim.crypto.SymmetricCipherImplTest.java

License:Apache License

/**
 * Test AES cipher mode/* w w  w .ja va2 s  .  c  o m*/
 */
public void testAESMode(short keyLen, byte mode, String[] testData) {
    short keyLenInBytes = (short) (keyLen / 8);
    Cipher engine = Cipher.getInstance(mode, false);
    AESKey aesKey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES, keyLen, false);
    byte[] etalonKey = Hex.decode(testData[0]);
    byte[] key = new byte[keyLenInBytes];
    Util.arrayCopy(etalonKey, (short) 0, key, (short) 0, (short) etalonKey.length);
    aesKey.setKey(key, (short) 0);
    boolean needIV = (mode == Cipher.ALG_AES_BLOCK_128_CBC_NOPAD);
    if (needIV) {
        byte[] iv = Hex.decode(testData[1]);
        engine.init(aesKey, Cipher.MODE_ENCRYPT, iv, (short) 0, (short) iv.length);
    } else {
        engine.init(aesKey, Cipher.MODE_ENCRYPT);
    }
    byte[] encrypted = new byte[16]; // AES 128
    short processedBytes = engine.doFinal(Hex.decode(testData[needIV ? 2 : 1]), (short) 0, (short) 16,
            encrypted, (short) 0);
    assertEquals(processedBytes, 16);
    assertEquals(true, Arrays.areEqual(encrypted, Hex.decode(testData[needIV ? 3 : 2])));
    if (needIV) {
        byte[] iv = Hex.decode(testData[1]);
        engine.init(aesKey, Cipher.MODE_DECRYPT, iv, (short) 0, (short) iv.length);
    } else {
        engine.init(aesKey, Cipher.MODE_DECRYPT);
    }
    byte[] decrypted = new byte[16]; // AES 128
    processedBytes = engine.doFinal(Hex.decode(testData[needIV ? 3 : 2]), (short) 0, (short) 16, decrypted,
            (short) 0);
    assertEquals(processedBytes, 16);
    assertEquals(true, Arrays.areEqual(decrypted, Hex.decode(testData[needIV ? 2 : 1])));
}

From source file:com.licel.jcardsim.crypto.SymmetricCipherImplTest.java

License:Apache License

/**
 * Test method// w  ww .  j av a2 s . co  m
 * <code>doFinal</code>
 *
 * @param engine test engine
 * @param key etalon key
 * @param iv IV if present
 * @param msg etalon message
 * @param encryptedEtalonMsg encrypted etalon message
 */
public void testEngineDoFinal(Cipher engine, Key key, byte[] iv, byte[] msg, byte[] encryptedEtalonMsg) {
    // first test equals encryption
    if (iv == null) {
        engine.init(key, Cipher.MODE_ENCRYPT);
    } else {
        engine.init(key, Cipher.MODE_ENCRYPT, iv, (short) 0, (short) iv.length);
    }
    byte[] encrypted = new byte[encryptedEtalonMsg.length];
    short processedBytes = engine.doFinal(msg, (short) 0, (short) msg.length, encrypted, (short) 0);
    assertEquals(true, Arrays.areEqual(encrypted, encryptedEtalonMsg));
    assertEquals(processedBytes, encryptedEtalonMsg.length);
    // second test decryption
    if (iv == null) {
        engine.init(key, Cipher.MODE_DECRYPT);
    } else {
        engine.init(key, Cipher.MODE_DECRYPT, iv, (short) 0, (short) iv.length);
    }
    byte[] decrypted = new byte[msg.length];
    processedBytes = engine.doFinal(encryptedEtalonMsg, (short) 0, (short) encryptedEtalonMsg.length, decrypted,
            (short) 0);
    assertEquals(processedBytes, msg.length);
    assertEquals(true, Arrays.areEqual(decrypted, msg));
}

From source file:com.licel.jcardsim.crypto.SymmetricKeyImplTest.java

License:Apache License

/**
 * Test of getKey method, of class SymmetricKeyImpl.
 *//*from  w w  w . ja  va2s  .co  m*/
public void testGetKey() {
    System.out.println("getKey");
    SymmetricKeyImpl desKey = new SymmetricKeyImpl(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES);
    byte[] key = new byte[8];
    Util.arrayFillNonAtomic(key, (short) 0, (short) key.length, (byte) 7);
    desKey.setKey(key, (short) 0);
    byte[] testKey = new byte[8];
    desKey.getKey(testKey, (short) 0);
    assertEquals(true, Arrays.areEqual(testKey, key));
}

From source file:com.licel.jcardsim.crypto.SymmetricSignatureImplTest.java

License:Apache License

/**
 * Test of sign/verify methods, of class SymmetricSignatureImpl with specified key, engine
 * and etalon data//from www  .j a  va  2  s .  c  o  m
 * @param engine test engine
 * @param key etalon key
 * @param iv IV if present
 * @param msg etalon msg
 * @param macEtalon etalon signature(mac)
 */
public void testEngineSignVerify(Signature engine, Key key, byte[] iv, byte[] msg, byte[] macEtalon) {
    // sign
    if (iv == null) {
        engine.init(key, Signature.MODE_SIGN);
    } else {
        engine.init(key, Signature.MODE_SIGN, iv, (short) 0, (short) iv.length);
    }
    byte[] mac = new byte[macEtalon.length];
    //
    engine.sign(msg, (short) 0, (short) msg.length, mac, (short) 0);
    assertEquals(true, Arrays.areEqual(mac, macEtalon));
    // verify
    if (iv == null) {
        engine.init(key, Signature.MODE_VERIFY);
    } else {
        engine.init(key, Signature.MODE_VERIFY, iv, (short) 0, (short) iv.length);
    }
    assertEquals(true,
            engine.verify(msg, (short) 0, (short) msg.length, macEtalon, (short) 0, (short) macEtalon.length));

}

From source file:com.mirth.connect.plugins.mllpmode.MLLPv2StreamHandler.java

License:Open Source License

@Override
public void write(byte[] data) throws IOException {
    boolean done = false;
    Exception firstCause = null;//from w w w.  ja  v a 2s.co m
    int retryCount = 0;

    while (!done) {
        // Write the data as per normal MLLPv1
        super.write(data);

        try {
            // Attempt to retrieve an acknowledgement
            byte[] response = super.read();
            // Reset the streamDone flag so the input stream can be read from again
            reset();

            if (response == null) {
                /*
                 * Nothing was captured but an exception also was not thrown, so assume this is
                 * due to a previous read encountering an EOF and marking the stream as done.
                 * Reset the FrameStreamHandler's flags and try once more.
                 */
                response = super.read();
            }

            if (Arrays.areEqual(response, ackBytes)) {
                done = true;
            } else if (Arrays.areEqual(response, nackBytes)) {
                throw new MLLPv2StreamHandlerException("Negative commit acknowledgement received.");
            } else {
                throw new MLLPv2StreamHandlerException("Invalid acknowledgement block received.");
            }
        } catch (IOException e) {
            if (firstCause == null) {
                firstCause = e;
            }

            if (maxRetries > 0 && retryCount++ == maxRetries) {
                throw new MLLPv2StreamHandlerException(
                        "Maximum retry count reached. First cause: " + firstCause.getMessage(), firstCause);
            }
        }
    }
}