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:applet.Applet7ZipTest.java

@Test
public void generateMasterKeyCtrZero() throws Exception {
    byte[] deriveNewKey = { Applet7Zip.CLA_7ZIPAPPLET, Applet7Zip.INS_DERIVE_NEW_KEY, 0x00, 0x00, 0x00 };
    byte[] generateKey = { Applet7Zip.CLA_7ZIPAPPLET, Applet7Zip.INS_GENERATE_MASTER_KEY, 0x00, 0x00, 0x00 };
    byte[] retrieveCtr = { Applet7Zip.CLA_7ZIPAPPLET, Applet7Zip.INS_RETRIEVE_CURRENT_CTR, 0x00, 0x00, 0x00 };
    byte[] expectedCtr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x90, 0x00 };
    byte[] recv;//w w w. j ava  2s  . co m
    recv = manager.transmitAPDU(VALID_USER_LOGIN);
    assertTrue(cmpResponseCode(recv, ISO7816.SW_NO_ERROR));
    recv = manager.transmitAPDU(deriveNewKey);
    assertTrue(cmpResponseCode(recv, ISO7816.SW_NO_ERROR));
    recv = manager.transmitAPDU(deriveNewKey);
    assertTrue(cmpResponseCode(recv, ISO7816.SW_NO_ERROR));
    recv = manager.transmitAPDU(deriveNewKey);
    assertTrue(cmpResponseCode(recv, ISO7816.SW_NO_ERROR));
    recv = manager.transmitAPDU(generateKey);
    assertTrue(cmpResponseCode(recv, ISO7816.SW_NO_ERROR));
    recv = manager.transmitAPDU(retrieveCtr);
    assertTrue(cmpResponseCode(recv, ISO7816.SW_NO_ERROR));
    assertTrue(Arrays.areEqual(recv, expectedCtr));
}

From source file:applet.Applet7ZipTest.java

@Test
public void deriveCtrKeyIsValid() throws Exception {
    byte[] deriveNew = { Applet7Zip.CLA_7ZIPAPPLET, Applet7Zip.INS_DERIVE_NEW_KEY, 0x00, 0x00, 0x00 };
    byte[] deriveCtr = { Applet7Zip.CLA_7ZIPAPPLET, Applet7Zip.INS_DERIVE_CTR_KEY, 0x00, 0x00,
            Applet7Zip.SIZE_COUNTER_BYTE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
    manager.transmitAPDU(VALID_USER_LOGIN);
    byte[] rval1 = manager.transmitAPDU(deriveNew);
    byte[] rval2 = manager.transmitAPDU(deriveCtr);
    assertTrue(Arrays.areEqual(rval1, rval2));
}

From source file:applet.Applet7ZipTest.java

/**
 * Derives two new keys and then tries to access them with extracted counters.
 * Repeat this many times./*  w w w  . j  a va 2  s.co m*/
 * @throws Exception 
 */
@Test
public void derivedKeysTest() throws Exception {
    byte[] deriveNewKey = { Applet7Zip.CLA_7ZIPAPPLET, Applet7Zip.INS_DERIVE_NEW_KEY, 0x00, 0x00, 0x00 };

    byte[] deriveCtrKey = new byte[13];
    deriveCtrKey[ISO7816.OFFSET_CLA] = Applet7Zip.CLA_7ZIPAPPLET;
    deriveCtrKey[ISO7816.OFFSET_INS] = Applet7Zip.INS_DERIVE_CTR_KEY;
    deriveCtrKey[ISO7816.OFFSET_P1] = 0x00;
    deriveCtrKey[ISO7816.OFFSET_P2] = 0x00;
    deriveCtrKey[ISO7816.OFFSET_LC] = Applet7Zip.SIZE_COUNTER_BYTE;

    byte[] getCounter = { Applet7Zip.CLA_7ZIPAPPLET, Applet7Zip.INS_RETRIEVE_CURRENT_CTR, 0x00, 0x00, 0x00 };

    /* User login */
    byte[] recv = manager.transmitAPDU(VALID_USER_LOGIN);
    assertTrue(cmpResponseCode(recv, ISO7816.SW_NO_ERROR));

    byte[] firstKey;
    byte[] secondKey;
    byte[] counterKey;
    byte[] counter;

    for (int i = 0; i < 1000; ++i) {
        /* Get first key */
        firstKey = manager.transmitAPDU(deriveNewKey);
        /* Get counter of first key */
        counter = manager.transmitAPDU(getCounter);
        /* Get second key */
        secondKey = manager.transmitAPDU(deriveNewKey);
        /* Append counter of first key to deriveCrtKey */
        System.arraycopy(counter, 0, deriveCtrKey, ISO7816.OFFSET_CDATA, Applet7Zip.SIZE_COUNTER_BYTE);
        /* Derive key from counter */
        counterKey = manager.transmitAPDU(deriveCtrKey);
        /* Compare first key to counter key */
        assertTrue(Arrays.areEqual(firstKey, counterKey));
        /* Check that first derivation succeeded */
        assertTrue(cmpResponseCode(firstKey, ISO7816.SW_NO_ERROR));
        /* Get counter of second key */
        counter = manager.transmitAPDU(getCounter);
        /* Append second counter to deriveCtrKey */
        System.arraycopy(counter, 0, deriveCtrKey, ISO7816.OFFSET_CDATA, Applet7Zip.SIZE_COUNTER_BYTE);
        /* Derive key from counter */
        counterKey = manager.transmitAPDU(deriveCtrKey);
        /* Compare second key to counter key */
        assertTrue(Arrays.areEqual(secondKey, counterKey));
        /* Check that second derivation succeeded */
        assertTrue(cmpResponseCode(secondKey, ISO7816.SW_NO_ERROR));
    }
}

From source file:be.fedict.eid.applet.service.signer.SHA1WithRSAProxySignature.java

License:Open Source License

@Override
protected void engineUpdate(byte[] b, int off, int len) throws SignatureException {
    LOG.debug("engineUpdate(b,off,len): off=" + off + "; len=" + len);
    this.messageDigest.update(b, off, len);
    byte[] digestValue = this.messageDigest.digest();
    byte[] expectedDigestValue = SHA1WithRSAProxySignature.digestValues.get();
    if (null == expectedDigestValue) {
        SHA1WithRSAProxySignature.digestValues.set(digestValue);
    } else {/*from  w w w.  ja  v a2s .  co  m*/
        if (false == Arrays.areEqual(expectedDigestValue, digestValue)) {
            throw new IllegalStateException("digest value has changed");
        }
    }
    LOG.debug("digest value: " + Hex.encodeHexString(digestValue));
}

From source file:ca.sqlpower.wabit.dao.session.PNGImageConverterTest.java

License:Open Source License

public void testConvertToAndFromImage() throws Exception {
    PNGImageConverter converter = new PNGImageConverter();

    //random image for testing
    Image img = new ImageIcon(PNGImageConverter.class.getClassLoader().getResource("icons/execute.png"))
            .getImage();/*from  w  w  w . j av a 2s .c om*/
    ByteArrayOutputStream buffer = PersisterUtils.convertImageToStreamAsPNG(img);

    System.out.println("Start buffer " + buffer);

    InputStream inputStream = converter.convertToSimpleType(img);

    Image newImg = converter.convertToComplexType(inputStream);
    ByteArrayOutputStream newBuffer = PersisterUtils.convertImageToStreamAsPNG(newImg);

    System.out.println("End buffer " + newBuffer);

    assertTrue(Arrays.areEqual(buffer.toByteArray(), newBuffer.toByteArray()));

}

From source file:co.runrightfast.core.security.auth.x500.DistinguishedNameTest.java

License:Apache License

@Test
public void test_toX500Principal() {
    final DistinguishedName dn = DistinguishedName.builder().commonName("Alfio Zappala").country("US")
            .domain("www.runrightfast.co").localityName("Rochester").organizationName("RunRightFast.co")
            .organizationalUnitName("Executive").stateOrProvinceName("NY").streetAddress("123 Main St.")
            .userid("0123456789").build();

    log.info(String.format("dn =\n%s", dn));
    final X500Principal principal = dn.toX500Principal();
    log.info(String.format("principle name =\n%s", principal.getName()));

    final X500Principal principal2 = new X500Principal(principal.getEncoded());
    log.info(String.format("principle2 name =\n%s", principal2.getName()));
    log.info(String.format("principle2 name RFC2253 =\n%s", principal2.getName(X500Principal.RFC2253)));

    assertThat(Arrays.areEqual(principal.getEncoded(), principal2.getEncoded()), is(true));
    assertThat(principal, is(principal2));
}

From source file:co.runrightfast.core.security.auth.x500.DistinguishedNameTest.java

License:Apache License

@Test
public void test_toX500Name() throws IOException {
    final DistinguishedName dn = DistinguishedName.builder().commonName("Alfio Zappala").country("US")
            .domain("www.runrightfast.co").localityName("Rochester").organizationName("RunRightFast.co")
            .organizationalUnitName("Executive").stateOrProvinceName("NY").streetAddress("123 Main St.")
            .userid("0123456789").build();

    log.info(String.format("dn =\n%s", dn));
    final X500Principal principal = dn.toX500Principal();
    log.info(String.format("principle name =\n%s", principal.getName()));

    final X500Principal principal2 = new X500Principal(principal.getEncoded());
    log.info(String.format("principle2 name =\n%s", principal2.getName()));
    log.info(String.format("principle2 name RFC2253 =\n%s", principal2.getName(X500Principal.RFC2253)));

    assertThat(Arrays.areEqual(principal.getEncoded(), principal2.getEncoded()), is(true));
    assertThat(principal, is(principal2));

    final X500Name name1 = DistinguishedName.toX500Name(principal);
    final X500Name name2 = DistinguishedName.toX500Name(principal2);

    log.info(String.format("name1 : %s", name1));
    log.info(String.format("name2 : %s", name2));

    assertThat(Arrays.areEqual(name1.getEncoded(), name1.getEncoded()), is(true));
}

From source file:co.runrightfast.core.security.bc.SHA512DigestCalculatorTest.java

License:Apache License

@Test
public void testGetDigest() throws IOException, NoSuchAlgorithmException, NoSuchProviderException {
    final SHA512DigestCalculator sha512 = new SHA512DigestCalculator();
    final byte[] bytes = getClass().getName().getBytes();
    sha512.getOutputStream().write(bytes);
    final byte[] digest = sha512.getDigest();

    final MessageDigest messageDigest = MessageDigest.getInstance("SHA-512", BOUNCY_CASTLE);
    final byte[] digest2 = messageDigest.digest(bytes);

    assertThat(Arrays.areEqual(digest, digest2), is(true));

    final MessageDigest messageDigest2 = MessageDigest.getInstance("SHA-512");
    final byte[] digest3 = messageDigest2.digest(bytes);

    assertThat(Arrays.areEqual(digest, digest3), is(true));

}

From source file:co.runrightfast.core.security.cert.impl.CertificateServiceImplTest.java

License:Apache License

@Test
public void testGenerateX509CertificateV1()
        throws NoSuchAlgorithmException, NoSuchProviderException, CertificateExpiredException,
        CertificateNotYetValidException, CertificateException, InvalidKeyException, SignatureException {
    final DistinguishedName issuer = issuer();

    final X500Principal issuerPrincipal = issuer.toX500Principal();

    final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA.name(), BOUNCY_CASTLE);
    final KeyPair signingKeyPair = keyPairGenerator.generateKeyPair();

    final KeyPair certKeyPair = keyPairGenerator.generateKeyPair();

    final X509V1CertRequest request = new X509V1CertRequest(issuerPrincipal, BigInteger.ONE, Instant.now(),
            Instant.ofEpochMilli(System.currentTimeMillis() + (10 * 1000)), issuerPrincipal,
            certKeyPair.getPublic());//from  w  w  w  .  j a  v a 2s .  co  m
    log.info(String.format("request : %s", request));

    final X509Certificate cert = certificateService.generateX509CertificateV1(request,
            signingKeyPair.getPrivate());
    log.info(String.format("result.getSigAlgName() = %s, result.getVersion() = %s ", cert.getSigAlgName(),
            cert.getVersion()));
    assertThat(cert.getVersion(), is(1));

    cert.checkValidity();
    assertThat(Arrays.areEqual(issuerPrincipal.getEncoded(), cert.getIssuerX500Principal().getEncoded()),
            is(true));
    cert.verify(signingKeyPair.getPublic());

}

From source file:co.runrightfast.core.security.cert.impl.CertificateServiceImplTest.java

License:Apache License

@Test
public void test_generateSelfSignedX509CertificateV1()
        throws NoSuchAlgorithmException, NoSuchProviderException, CertificateExpiredException,
        CertificateNotYetValidException, CertificateException, InvalidKeyException, SignatureException {
    final DistinguishedName issuer = issuer();

    final X500Principal principal = issuer.toX500Principal();

    final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA.name(), BOUNCY_CASTLE);
    final KeyPair keyPair = keyPairGenerator.generateKeyPair();

    final SelfSignedX509V1CertRequest request = new SelfSignedX509V1CertRequest(principal, BigInteger.ONE,
            Instant.now(), Instant.ofEpochMilli(System.currentTimeMillis() + (10 * 1000)), keyPair);
    log.info(String.format("request : %s", request));

    final X509Certificate cert = certificateService.generateSelfSignedX509CertificateV1(request);
    log.info(String.format("result.getSigAlgName() = %s, result.getVersion() = %s ", cert.getSigAlgName(),
            cert.getVersion()));/*from  w  w w. j  av  a 2 s  .  com*/
    assertThat(cert.getVersion(), is(1));

    cert.checkValidity();
    assertThat(Arrays.areEqual(principal.getEncoded(), cert.getIssuerX500Principal().getEncoded()), is(true));
    cert.verify(cert.getPublicKey());
}