Example usage for org.apache.commons.codec.binary Hex decodeHex

List of usage examples for org.apache.commons.codec.binary Hex decodeHex

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Hex decodeHex.

Prototype

public static byte[] decodeHex(char[] data) throws IllegalArgumentException 

Source Link

Document

Converts an array of characters representing hexadecimal values into an array of bytes of those same values.

Usage

From source file:libepg.util.bytearray.ByteArraySplitterTest.java

/**
 * Test of split method, of class ByteArraySplitter.
 *
 * @throws org.apache.commons.codec.DecoderException
 *//*from w  ww . j av  a 2s .  c o m*/
@Test
public void testSplit() throws DecoderException {
    LOG.debug("split");
    byte[] src = Hex.decodeHex("00112233445566778899aabbccddeeff".toCharArray());
    int size = 3;
    List<byte[]> expResult = new ArrayList<>();
    expResult.add(Hex.decodeHex("001122".toCharArray()));
    expResult.add(Hex.decodeHex("334455".toCharArray()));
    expResult.add(Hex.decodeHex("667788".toCharArray()));
    expResult.add(Hex.decodeHex("99aabb".toCharArray()));
    expResult.add(Hex.decodeHex("ccddee".toCharArray()));
    expResult.add(Hex.decodeHex("ff0000".toCharArray()));
    List result = ByteArraySplitter.split(src, size);

    Iterator<byte[]> it_result = result.iterator();
    Iterator<byte[]> it_expResult = expResult.iterator();
    while (it_result.hasNext() && it_expResult.hasNext()) {
        StringBuilder s = new StringBuilder();
        byte[] res = it_result.next();
        byte[] expRes = it_expResult.next();
        if (Arrays.equals(res, expRes) == false) {
            fail("???????");
        } else {
            s.append(Hex.encodeHexString(expRes));
            s.append(" = ");
            s.append(Hex.encodeHexString(res));
            LOG.debug(s.toString());
        }
    }
}

From source file:libepg.epg.section.descriptor.DescriptorsLoopTest.java

public DescriptorsLoopTest() throws DecoderException {
    target = new DescriptorsLoop(
            Hex.decodeHex("481201000f0e4e484b451d461d6c310f456c357ec10184cf0701fe08f0010408".toCharArray()));
    target2 = new DescriptorsLoop(
            Hex.decodeHex("481201000f0e4e484b451d461d6c320f456c357ec10184cf0302fe08".toCharArray()));
    descs = new Descriptors();
    descsList = new ArrayList<>();
    descsList.add(new ServiceDescriptor(descs.getSERVICE_DESCRIPTOR()));
    descsList.add(descs.getDIGITAL_COPY_CONTROL_DESCRIPTOR());
    descsList.add(descs.getLOGO_TRANSMISSION_DESCRIPTOR());
}

From source file:com.alu.e3.prov.lifecycle.IDHelper.java

public static String decode(String encoded) {
    try {//from w  w w .  j  a  va2  s  .c o m
        return new String(Hex.decodeHex(encoded.toCharArray()));
    } catch (DecoderException e) {
        throw new RuntimeException(e);
    }
}

From source file:architecture.common.license.validator.CheckSignatureValidator.java

public void validate(License license) throws LicenseException {
    try {//  www. j ava 2s  .co  m
        //   DSA ? .
        String publicKey = "308201b83082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a0381850002818100faf2d25b2866aa68501094d1097bebc95c6bcf1c58766f18b35fbf5e9d761cc5bf913447e374c21d279777859f9f043d1dc0d58b93a2081b56b4f5269a81b076907a3b11b01ec5cfde5dae4dfd7d26346e53e611235e714e69ec1bc141c77a8a28c4c799df570a4c3240e7f2fee19d6ed4caaa1b15b5da4a967ee82e3eb4d4ca";

        byte pub[] = Hex.decodeHex(publicKey.toCharArray());
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pub);

        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
        Signature sig = Signature.getInstance("DSA");

        sig.initVerify(pubKey);
        // license.getSignature().getBytes("UTF-8");
        byte decoded[] = Hex.decodeHex(license.getSignature().toCharArray());

        log.debug("decoded sig: " + Hex.encodeHexString(decoded));

        log.info((new StringBuilder()).append("Validating license. License fingerprint: ")
                .append(license.getSignature()).toString());

        sig.update(license.getFingerprint());

        boolean verified = sig.verify(decoded);

        if (!verified)
            throw new LicenseException("License signature is invalid.");
    } catch (Exception e) {
        log.fatal(e.getMessage(), e);
        throw new LicenseException(e);
    }
}

From source file:com.tc.simple.apn.factories.PushByteFactory.java

@Override
public byte[] buildPushBytes(int id, Payload payload) {
    byte[] byteMe = null;
    ByteArrayOutputStream baos = null;
    DataOutputStream dos = null;/*w  w  w . ja va  2 s  .c o  m*/

    try {
        baos = new ByteArrayOutputStream();

        dos = new DataOutputStream(baos);

        int expiry = 0; // (int) ((System.currentTimeMillis () / 1000L) + 7200);

        char[] cars = payload.getToken().trim().toCharArray();

        byte[] tokenBytes = Hex.decodeHex(cars);

        //command
        dos.writeByte(1);

        //id
        dos.writeInt(id);

        //expiry
        dos.writeInt(expiry);

        //token length.
        dos.writeShort(tokenBytes.length);

        //token
        dos.write(tokenBytes);

        //payload length
        dos.writeShort(payload.getJson().length());

        logger.log(Level.FINE, payload.getJson());

        //payload.
        dos.write(payload.getJson().getBytes());

        byteMe = baos.toByteArray();

    } catch (Exception e) {
        logger.log(Level.SEVERE, null, e);

    } finally {
        CloseUtils.close(dos);
        CloseUtils.close(baos);
    }

    return byteMe;

}

From source file:me.footlights.core.crypto.SecretKeyTest.java

/**
 * Test encryption and decryption using test vectors from
 * @url http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip.
 *///from  ww w  .  j  a  va2 s .co  m
@Test
public void testEncryptDecrypt() throws Throwable {
    for (String[] v : TEST_VECTORS) {
        int i = 0;

        String algorithm = v[i++];
        String mode = v[i++];
        byte[] secret = Hex.decodeHex(v[i++].toCharArray());
        i++; // We don't actually use the IV.

        byte[] plaintext = Hex.decodeHex(v[i++].toCharArray());
        ByteBuffer plainbuf = ByteBuffer.wrap(Arrays.copyOf(plaintext, plaintext.length));

        byte[] ciphertext = Hex.decodeHex(v[i++].toCharArray());
        ByteBuffer cipherbuf = ByteBuffer.wrap(Arrays.copyOf(ciphertext, ciphertext.length));

        SecretKey key = SecretKey.newGenerator().setAlgorithm(algorithm).setBytes(secret).generate();

        CipherBuilder builder = key.newCipherBuilder().setMode(mode);

        try {
            Cipher e = builder.setOperation(Operation.ENCRYPT).build();
            Cipher d = builder.setOperation(Operation.DECRYPT).build();

            assertArrayEquals(ciphertext, e.doFinal(plaintext));
            assertArrayEquals(plaintext, d.doFinal(ciphertext));

            // Do it again, but this time with ByteBuffers.
            e.doFinal(plainbuf, cipherbuf);
            assertArrayEquals(ciphertext, cipherbuf.array());

            d.doFinal(cipherbuf, plainbuf);
            assertArrayEquals(plaintext, plainbuf.array());
        } catch (InvalidKeyException e) {
            fail("Unable to construct an AES cipher with a " + (8 * secret.length)
                    + "-bit key; is the JCE unlimited strength policy installed?");
        }
    }
}

From source file:de.phoenix.security.SaltedPassword.java

public byte[] getSaltHex() throws DecoderException {
    return Hex.decodeHex(salt.toCharArray());

}

From source file:com.knewton.mapreduce.io.StudentEventWritableTest.java

@Test
public void testSerialization() throws DecoderException, TException, IOException {
    byte[] studentEventDataBytes = Hex.decodeHex(eventDataString.toCharArray());
    StudentEventData studentEventData = new StudentEventData();
    deserializer.deserialize(studentEventData, studentEventDataBytes);
    StudentEvent studentEvent = new StudentEvent(1, studentEventData);
    StudentEventWritable sew = new StudentEventWritable(studentEvent, 10L);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(byteArrayOutputStream);
    sew.write(dout);/*from  www .  ja  va2s. c  o m*/

    StudentEventWritable deserializedWritable = new StudentEventWritable();
    InputStream is = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    DataInputStream dis = new DataInputStream(is);
    deserializedWritable.readFields(dis);
    assertEquals(sew.getStudentEvent(), deserializedWritable.getStudentEvent());
    assertEquals(sew.getTimestamp(), deserializedWritable.getTimestamp());
}

From source file:net.sheehantech.cherry.ProtocolTest.java

@Test
public void binaryEncodeShortBasic() throws Exception {
    String expectedContent = "{\"aps\":{\"alert\":\"0\"}}";
    Notification notification = new Notification();
    notification.setToken("00");
    notification.setAPS(new BasicAPSInfo("0"));
    byte[] encodedPayload = Protocol.legacyEncode(notification);
    //System.out.println("Length: " + encodedPayload.length);
    //for (byte b : encodedPayload) System.out.println(b);
    byte[] expected = expected((byte) 0, Hex.decodeHex("00".toCharArray()), expectedContent.getBytes());
    assertEquals(expected.length, encodedPayload.length);
    for (int b = 0; b < encodedPayload.length; b++) {
        //System.out.println("Checking byte " + b);
        assertEquals(expected[b], encodedPayload[b]);
    }//w  w  w .j  a v  a 2  s  . c o m
}

From source file:com.vvote.verifierlibrary.utils.Utils.java

/**
 * Decodes a string in hexadecimal format into byte format
 * //from  w  ww. j  ava2s. c o  m
 * @param data
 * @return byte data
 */
public static byte[] decodeHexData(String data) {
    try {
        return Hex.decodeHex(data.toCharArray());
    } catch (DecoderException e) {
        logger.error("Unable to decode hex data from string: {}", data);
    }

    return null;
}