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:com.cyanogenmod.account.util.CMAccountUtils.java

public static byte[] decodeHex(String hex) {
    try {//w  w w.j  a  v  a2s  . c  om
        return Hex.decodeHex(hex.toCharArray());
    } catch (DecoderException e) {
        Log.e(TAG, "Unable to decode hex string", e);
        throw new AssertionError(e);
    }
}

From source file:libepg.epg.section.SectionTest.java

/**
 * Test of getData method, of class Section.
 *///from w w w  .  j  a v  a2s. c  o  m
@Test
public void testGetData() throws DecoderException {
    LOG.debug("getData");
    Section instance = testSection_OK;
    byte[] expResult = Hex.decodeHex(
            "42f0977fe1d100007fe1ff0408f30020481201000f0e4e484b451d461d6c310f456c357ec10184cf0701fe08f00104080409f3001c481201000f0e4e484b451d461d6c320f456c357ec10184cf0302fe08040af3001c481201000f0e4e484b451d461d6c330f456c357ec10184cf0302fe080588e5001f480ec0000b0e4e484b0f374842530e32c10188cf0a030e4e484b0f215d0e32722fa2b5"
                    .toCharArray());
    byte[] result = instance.getData();
    //        LOG.debug(Hex.encodeHexString(expResult));
    //        LOG.debug(Hex.encodeHexString(result));
    assertArrayEquals(expResult, result);

}

From source file:com.lightboxtechnologies.spectrum.ExtractMapper.java

protected void process_extent(FSDataInputStream file, FileSystem fs, Path outPath, Map<String, ?> map,
        Context context) throws IOException, InterruptedException {
    final String id = (String) map.get("id");
    byte[] id_b = null;
    try {/*w  w  w .  ja v a2 s.c om*/
        id_b = Hex.decodeHex(id.toCharArray());
    } catch (DecoderException e) {
        throw new RuntimeException(e);
    }

    final long fileSize = ((Number) map.get("size")).longValue();
    StringBuilder sb = new StringBuilder("Extracting ");
    sb.append(id);
    sb.append(":");
    sb.append((String) map.get("fp"));
    sb.append(" (");
    sb.append(fileSize);
    sb.append(" bytes)");
    LOG.info(sb.toString());
    MD5Hash.reset();

    final Map<String, Object> rec = fileSize > SIZE_THRESHOLD
            ? process_extent_large(file, fs, outPath, map, context)
            : process_extent_small(file, fileSize, map, context);

    // check if the md5 is known
    final byte[] md5 = hash_lookup_and_mark(rec, "md5");

    // check if the sha1 is known
    final byte[] sha1 = hash_lookup_and_mark(rec, "sha1");

    // write the entry to the file table
    EntryTbl.put(
            FsEntryHBaseOutputFormat.FsEntryHBaseWriter.createPut(id_b, rec, HBaseTables.ENTRIES_COLFAM_B));

    // write the md5 version of the key for the hash table
    OutKey.set(KeyUtils.makeEntryKey(md5, (byte) 0, id_b));
    final KeyValue ovMD5 = new KeyValue(OutKey.get(), HBaseTables.HASH_COLFAM_B, ingest_col, timestamp, empty);
    context.write(OutKey, ovMD5);

    // write the sha1 version of the key for the hash table
    OutKey.set(KeyUtils.makeEntryKey(sha1, (byte) 1, id_b));
    final KeyValue ovSHA1 = new KeyValue(OutKey.get(), HBaseTables.HASH_COLFAM_B, ingest_col, timestamp, empty);
    context.write(OutKey, ovSHA1);
}

From source file:com.glaf.core.security.RSAUtils.java

/**
 * ?16RSA?//ww w  .j  ava  2  s  .c  om
 * 
 * @param modulus
 *            
 * @param privateExponent
 *            
 * @return RSA?
 */
public static RSAPrivateKey getRSAPrivateKey(String hexModulus, String hexPrivateExponent) {
    if (StringUtils.isEmpty(hexModulus) || StringUtils.isEmpty(hexPrivateExponent)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "hexModulus and hexPrivateExponent cannot be empty. RSAPrivateKey value is null to return.");
        }
        return null;
    }
    byte[] modulus = null;
    byte[] privateExponent = null;
    try {
        modulus = Hex.decodeHex(hexModulus.toCharArray());
        privateExponent = Hex.decodeHex(hexPrivateExponent.toCharArray());
    } catch (DecoderException ex) {
        LOGGER.error("hexModulus or hexPrivateExponent value is invalid. return null(RSAPrivateKey).");
    }
    if (modulus != null && privateExponent != null) {
        return generateRSAPrivateKey(modulus, privateExponent);
    }
    return null;
}

From source file:com.mastercard.mobile_api.bytes.ByteArray.java

/**
 * Instantiates a new byte array.//from   www.  ja va 2s . c  o  m
 *
 * @param hexString the hexString
 */
private ByteArray(final String hexString) {
    // We add one zero at the beginning to support odd HEX strings
    final String adjustedHex;
    if ((hexString.length() % 2) == 0) {
        adjustedHex = hexString;
    } else {
        adjustedHex = "0" + hexString;
    }
    try {
        mData = Hex.decodeHex(hexString.toCharArray());
    } catch (DecoderException e) {
        throw new IllegalArgumentException("Invalid HEX String: " + adjustedHex);
    }
}

From source file:com.moz.fiji.schema.util.JsonEntityIdParser.java

/**
 * Gets byte array from string entity id given in "hbase=" or "hbase_hex" format.
 *
 * @param stringEntityId representing the row to acquire byte array for.
 * @return byte array of entity id.//  w w w .j a  va  2s. c  o  m
 * @throws IOException if the ASCII-encoded hex was improperly formed.
 */
private static byte[] parseBytes(final String stringEntityId) throws IOException {
    if (stringEntityId.startsWith(HBASE_ROW_KEY_PREFIX)) {
        final String rowKeySubstring = stringEntityId.substring(HBASE_ROW_KEY_PREFIX.length());
        return Bytes.toBytesBinary(rowKeySubstring);
    } else if (stringEntityId.startsWith(HBASE_HEX_ROW_KEY_PREFIX)) {
        final String rowKeySubstring = stringEntityId.substring(HBASE_HEX_ROW_KEY_PREFIX.length());
        try {
            return Hex.decodeHex(rowKeySubstring.toCharArray());
        } catch (DecoderException de) {
            // Re-wrap decoder exception as IOException.
            throw new IOException(de.getMessage());
        }
    } else {
        throw new IllegalArgumentException("Passed string must be prefixed by hbase= or hbase_hex=.");
    }
}

From source file:com.envirover.spl.SPLGroungControlTest.java

private MAVLinkPacket getPacket(String data) throws DecoderException {
    Parser parser = new Parser();
    MAVLinkPacket packet = null;//from   www  . j a  v a2  s  .  c  o m

    for (byte b : Hex.decodeHex(data.toCharArray())) {
        packet = parser.mavlink_parse_char(b & 0xFF);
    }

    return packet;
}

From source file:be.fedict.eid.idp.webapp.ProtocolExitServlet.java

private byte[] getIdentifierSecret(RPEntity rp) {

    if (null == rp || null == rp.getIdentifierSecretKey() || rp.getIdentifierSecretKey().trim().isEmpty()) {
        // RP dont have one, go to IdP default
        return this.identityService.getHmacSecret();
    }//from w  w  w.  java 2s.  c  om

    try {
        return Hex.decodeHex(rp.getIdentifierSecretKey().toCharArray());
    } catch (DecoderException e) {
        throw new RuntimeException("HEX decoder error: " + e.getMessage(), e);
    }

}

From source file:com.cloudant.sync.datastore.encryption.EndToEndEncryptionTest.java

public static byte[] hexStringToByteArray(String s) {
    try {/*from w w  w . j  ava  2s . c om*/
        return Hex.decodeHex(s.toCharArray());
    } catch (DecoderException ex) {
        // Crash the tests at this point, we've input bad data in our hard-coded values
        throw new RuntimeException("Error decoding hex data: " + s);
    }
}

From source file:com.ning.arecibo.util.timeline.times.TestTimelineCoder.java

@Test(groups = "fast")
public void test65KRepeats() throws Exception {
    int count = 0;
    final List<DateTime> dateTimes = new ArrayList<DateTime>();
    final DateTime startingDateTime = DateTimeUtils.dateTimeFromUnixSeconds(1000000);
    DateTime time = startingDateTime;//www  .  j  av  a 2s .  c om
    for (int i = 0; i < 20; i++) {
        time = time.plusSeconds(200);
        dateTimes.add(time);
    }
    for (int i = 0; i < 0xFFFF + 100; i++) {
        time = time.plusSeconds(100);
        dateTimes.add(time);
    }
    final byte[] timeBytes = timelineCoder.compressDateTimes(dateTimes);
    final String hex = new String(Hex.encodeHex(timeBytes));
    // Here are the compressed samples: ff000f4308fe13c8fdffff64fe6464
    // Translation:
    // [ff 00 0f 43 08] means absolution time 1000000
    // [fe 13 c8] means repeat 19 times delta 200 seconds
    // [fd ff ff 64] means repeat 65525 times delta 100 seconds
    // [fe 64 64] means repeat 100 times delta 100 seconds
    Assert.assertEquals(timeBytes, Hex.decodeHex("ff000f4308fe13c8fdffff64fe6464".toCharArray()));
    final List<DateTime> restoredSamples = timelineCoder.decompressDateTimes(timeBytes);
    Assert.assertEquals(restoredSamples.size(), dateTimes.size());
    for (int i = 0; i < count; i++) {
        Assert.assertEquals(restoredSamples.get(i), DateTimeUtils.unixSeconds(dateTimes.get(i)));
    }
}