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.google.gerrit.server.account.HashedPassword.java

License:Apache License

public boolean checkPassword(String password) {
    // Constant-time comparison, because we're paranoid.
    return Arrays.areEqual(hashPassword(password, salt, cost), hashed);
}

From source file:com.igeekinc.indelible.indeliblefs.uniblock.casstore.mapdb.BlockInfo.java

License:Open Source License

public void testBasic() throws Exception {
    CASIDMemoryDataDescriptor.setMaxGeneratorThreads(8);
    MapDBFSCASStore testStore = createTempCASStore();
    byte[] dataA = new byte[1024];
    for (int index = 0; index < dataA.length; index++)
        dataA[index] = 'A';
    CASIDMemoryDataDescriptor storeDescriptor = new CASIDMemoryDataDescriptor(dataA);
    testStore.storeSegment(storeDescriptor);
    CASIDDataDescriptor restoreDescriptor = testStore.retrieveSegment(storeDescriptor.getCASIdentifier());
    byte[] retrievedData = restoreDescriptor.getData();
    assertTrue(Arrays.areEqual(dataA, retrievedData));
    testStore.shutdown();/*from w ww .  j a  v a2s  .co  m*/
    TestFilesTool.deleteTree(testStore.getStoreRoot());
}

From source file:com.joyent.manta.serialization.EncryptedMultipartUploaSerializationHelper.java

License:Open Source License

/**
 * Decrypts and deserializes the specified binary blob.
 *
 * @param serializedData data to decrypt and deserialize
 * @return an upload object/*from   w  w w  .j ava  2s  . co m*/
 */
@SuppressWarnings("unchecked")
public EncryptedMultipartUpload<WRAPPED> deserialize(final byte[] serializedData) {
    final byte[] iv = Arrays.copyOf(serializedData, cipherDetails.getIVLengthInBytes());

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Reading IV: {}", Hex.toHexString(iv));
    }

    final Cipher cipher = cipherDetails.getCipher();
    try {
        cipher.init(Cipher.DECRYPT_MODE, secretKey, cipherDetails.getEncryptionParameterSpec(iv));
    } catch (GeneralSecurityException e) {
        String msg = String.format("Unable to initialize cipher [%s]", cipherDetails.getCipherId());
        throw new MantaClientEncryptionException(msg, e);
    }

    final byte[] cipherText;

    if (cipherDetails.isAEADCipher()) {
        cipherText = extractCipherText(serializedData, iv.length, null);
    } else {
        final byte[] hmacIdBytes = Arrays.copyOfRange(serializedData,
                serializedData.length - CIPHER_ID_SIZE_BYTES, serializedData.length);
        final String hmacId = new String(hmacIdBytes, StandardCharsets.US_ASCII).trim();

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Verifying checksum with [{}]", hmacId);
        }

        final Supplier<HMac> hmacSupplier = SupportedHmacsLookupMap.INSTANCE.get(hmacId);

        if (hmacSupplier == null) {
            String msg = String.format("Unknown HMAC: [%s]", hmacId);
            throw new MantaClientEncryptionException(msg);
        }

        final HMac hmac = hmacSupplier.get();
        final int hmacLength = hmac.getMacSize();

        cipherText = extractCipherText(serializedData, iv.length, hmacLength);

        hmac.update(iv, 0, iv.length);
        hmac.update(cipherText, 0, cipherText.length);
        final byte[] calculated = new byte[hmacLength];
        hmac.doFinal(calculated, 0);

        final byte[] expected = Arrays.copyOfRange(serializedData,
                serializedData.length - hmacLength - CIPHER_ID_SIZE_BYTES,
                serializedData.length - CIPHER_ID_SIZE_BYTES);

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Expected HMAC:   {}", Hex.toHexString(expected));
            LOGGER.debug("Calculated HMAC: {}", Hex.toHexString(calculated));
        }

        if (!Arrays.areEqual(calculated, expected)) {
            String msg = "Serialization data ciphertext failed " + "cryptographic authentication";
            MantaClientEncryptionCiphertextAuthenticationException e = new MantaClientEncryptionCiphertextAuthenticationException(
                    msg);
            e.setContextValue("expected", Hex.toHexString(expected));
            e.setContextValue("calculated", Hex.toHexString(calculated));
            throw e;
        }
    }

    final byte[] plaintext;
    try {
        plaintext = cipher.doFinal(cipherText);
    } catch (GeneralSecurityException e) {
        String msg = "Error decrypting serialized object data";
        throw new MantaClientEncryptionException(msg, e);
    }

    final EncryptedMultipartUpload<WRAPPED> upload;

    try (Input input = new Input(plaintext)) {
        final int serializationVersion = input.readVarInt(true);

        if (serializationVersion != ENCRYPTED_MULTIPART_UPLOAD_SERIALIZATION_VERSION) {
            LOGGER.warn("Deserialized version [%d] is different than serialization version [%d",
                    serializationVersion, ENCRYPTED_MULTIPART_UPLOAD_SERIALIZATION_VERSION);
        }

        upload = (EncryptedMultipartUpload<WRAPPED>) kryo.readClassAndObject(input);
    }

    return upload;
}

From source file:com.licel.jcardsim.base.SimulatorTest.java

License:Apache License

/**
 * Test of installApplet method, of class Simulator.
 *///w w  w .ja  v a2s .  c o m
public void testInstallApplet_6args() {
    System.out.println("installApplet");
    Simulator instance = new Simulator();
    assertEquals(instance.installApplet(TEST_APPLET1_AID, TEST_APPLET1_CLASSNAME, appletJarContents, createData,
            (short) 0, (byte) createData.length).equals(TEST_APPLET1_AID), true);
    assertEquals(instance.selectApplet(TEST_APPLET1_AID), true);
    // test NOP
    byte[] response = instance.transmitCommand(new byte[] { 0x01, 0x02, 0x00, 0x00 });
    assertEquals(Arrays.areEqual(new byte[] { (byte) 0x90, 0x00 }, response), true);
}

From source file:com.licel.jcardsim.base.SimulatorTest.java

License:Apache License

/**
 * Test of transmitCommand method, of class Simulator.
 */// ww w. j  av  a 2s  .co m
public void testTransmitCommand() {
    System.out.println("transmitCommand");
    Simulator instance = new Simulator();
    instance.installApplet(TEST_APPLET_AID, TEST_APPLET_CLASS);
    assertEquals(instance.selectApplet(TEST_APPLET_AID), true);
    // test NOP
    byte[] response = instance.transmitCommand(new byte[] { 0x01, 0x02, 0x00, 0x00 });
    assertEquals(Arrays.areEqual(new byte[] { (byte) 0x90, 0x00 }, response), true);
}

From source file:com.licel.jcardsim.base.SimulatorTest.java

License:Apache License

/**
 * Test of getATR method, of class Simulator.
 *///  w ww  .j ava 2  s. c  o  m
public void testGetATR() {
    System.out.println("getATR");
    Simulator instance = new Simulator();
    byte[] result = instance.getATR();
    assertEquals(Arrays.areEqual(ETALON_ATR, result), true);
}

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

License:Apache License

/**
 * SelfTest of RSA Encryption/Decryption, of class AssymetricCipherImpl.
 *//*from   www . ja  va2 s.  com*/
public void testSelftRSA() {
    Cipher cipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
    KeyPair kp = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_1024);
    kp.genKeyPair();

    cipher.init(kp.getPublic(), Cipher.MODE_ENCRYPT);
    byte[] msg = JCSystem.makeTransientByteArray((short) 127, JCSystem.CLEAR_ON_RESET);
    byte[] encryptedMsg = JCSystem.makeTransientByteArray((short) 128, JCSystem.CLEAR_ON_RESET);
    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(kp.getPrivate(), Cipher.MODE_DECRYPT);
    byte[] decryptedMsg = JCSystem.makeTransientByteArray((short) msg.length, JCSystem.CLEAR_ON_RESET);
    cipher.doFinal(encryptedMsg, (short) 0, (short) encryptedMsg.length, decryptedMsg, (short) 0);

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

}

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

License:Apache License

/**
 * SelfTest of RSA Encryption/Decryption, of class AsymmetricCipherImpl.
 *///ww  w  .  jav a 2s .com
public void testSelftRSA() {
    Cipher cipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
    KeyPair kp = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_1024);
    kp.genKeyPair();

    cipher.init(kp.getPublic(), Cipher.MODE_ENCRYPT);
    byte[] msg = new byte[127];
    byte[] encryptedMsg = new byte[128];
    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(kp.getPrivate(), 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.CRC16Test.java

License:Apache License

/**
 * Test of of class CRC16.//from w w w.j  av a2 s.c o  m
 */
public void testCrc16() {
    System.out.println("test crc16");
    Checksum crcEngine = Checksum.getInstance(Checksum.ALG_ISO3309_CRC16, false);
    byte[] crc = new byte[2];
    byte[] msg = Hex.decode(MESSAGE);
    crcEngine.doFinal(msg, (short) 0, (short) msg.length, crc, (short) 0);
    assertEquals(true, Arrays.areEqual(Hex.decode(CRC), crc));
}

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

License:Apache License

/**
 * Test of of class CRC16./*  w  ww  .j  a  v a  2 s.  c o m*/
 */
public void testCrc32() {
    System.out.println("test crc32");
    Checksum crcEngine = Checksum.getInstance(Checksum.ALG_ISO3309_CRC32, false);
    byte[] crc = new byte[4];
    byte[] msg = Hex.decode(MESSAGE);
    crcEngine.doFinal(msg, (short) 0, (short) msg.length, crc, (short) 0);
    assertEquals(true, Arrays.areEqual(Hex.decode(CRC), crc));
}