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:org.apache.abdera.security.util.KeyHelper.java

public static PublicKey generatePublicKey(String hex) {
    try {//  ww w.  j  a  v  a  2  s  .  c o m
        if (hex == null || hex.trim().length() == 0)
            return null;
        byte[] data = Hex.decodeHex(hex.toCharArray());
        X509EncodedKeySpec keyspec = new X509EncodedKeySpec(data);
        KeyFactory keyfactory = KeyFactory.getInstance("RSA");
        return keyfactory.generatePublic(keyspec);
    } catch (Exception e) {
        return null;
    }
}

From source file:org.apache.abdera2.common.security.KeyHelper.java

public static SecretKeySpec decodeSecretKeySpec(String hex) {
    try {/* w  w  w .j a v a2s  . c  o  m*/
        return new SecretKeySpec(Hex.decodeHex(hex.toCharArray()), "RAW");
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:org.apache.ambari.view.hive.resources.jobs.StoredOperationHandle.java

public TOperationHandle toTOperationHandle() {
    TOperationHandle handle = new TOperationHandle();
    handle.setHasResultSet(isHasResultSet());
    handle.setModifiedRowCount(getModifiedRowCount());
    handle.setOperationType(TOperationType.findByValue(getOperationType()));
    THandleIdentifier identifier = new THandleIdentifier();
    try {/*from  www .j a v  a  2 s. com*/
        identifier.setGuid(Hex.decodeHex(getGuid().toCharArray()));
        identifier.setSecret(Hex.decodeHex(getSecret().toCharArray()));
    } catch (DecoderException e) {
        throw new ServiceFormattedException("Wrong identifer of OperationHandle is stored in DB");
    }
    handle.setOperationId(identifier);
    return handle;
}

From source file:org.apache.arrow.vector.file.json.JsonFileReader.java

private byte[] decodeHexSafe(String hexString) throws IOException {
    try {/*from   w ww .j a va 2  s .c  om*/
        return Hex.decodeHex(hexString.toCharArray());
    } catch (DecoderException e) {
        throw new IOException("Unable to decode hex string: " + hexString, e);
    }
}

From source file:org.apache.beam.sdk.extensions.sql.impl.udf.BuiltinStringFunctions.java

@UDF(funcName = "FROM_HEX", parameterArray = { TypeName.STRING }, returnType = TypeName.BYTES)
public byte[] fromHex(String str) {
    if (str == null) {
        return null;
    }//from   w w  w  .  ja  va  2s.  c  o  m

    try {
        return Hex.decodeHex(str.toCharArray());
    } catch (DecoderException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.carbondata.sdk.file.ImageTest.java

@Test
public void testBinaryWithOrWithoutFilter()
        throws IOException, InvalidLoadOptionException, InterruptedException, DecoderException {
    String imagePath = "./src/test/resources/image/carbondatalogo.jpg";
    int num = 1;/*from w w  w . ja  va2 s .com*/
    int rows = 1;
    String path = "./target/binary";
    try {
        FileUtils.deleteDirectory(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Field[] fields = new Field[3];
    fields[0] = new Field("name", DataTypes.STRING);
    fields[1] = new Field("age", DataTypes.INT);
    fields[2] = new Field("image", DataTypes.BINARY);

    byte[] originBinary = null;

    // read and write image data
    for (int j = 0; j < num; j++) {
        CarbonWriter writer = CarbonWriter.builder().outputPath(path).withCsvInput(new Schema(fields))
                .writtenBy("SDKS3Example").withPageSizeInMb(1).build();

        for (int i = 0; i < rows; i++) {
            // read image and encode to Hex
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(imagePath));
            char[] hexValue = null;
            originBinary = new byte[bis.available()];
            while ((bis.read(originBinary)) != -1) {
                hexValue = Hex.encodeHex(originBinary);
            }
            // write data
            writer.write(new String[] { "robot" + (i % 10), String.valueOf(i), String.valueOf(hexValue) });
            bis.close();
        }
        writer.close();
    }

    // Read data with filter
    EqualToExpression equalToExpression = new EqualToExpression(new ColumnExpression("name", DataTypes.STRING),
            new LiteralExpression("robot0", DataTypes.STRING));

    CarbonReader reader = CarbonReader.builder(path, "_temp").filter(equalToExpression).build();

    System.out.println("\nData:");
    int i = 0;
    while (i < 20 && reader.hasNext()) {
        Object[] row = (Object[]) reader.readNextRow();

        byte[] outputBinary = Hex.decodeHex(new String((byte[]) row[1]).toCharArray());
        System.out.println(row[0] + " " + row[2] + " image size:" + outputBinary.length);

        // validate output binary data and origin binary data
        assert (originBinary.length == outputBinary.length);
        for (int j = 0; j < originBinary.length; j++) {
            assert (originBinary[j] == outputBinary[j]);
        }
        String value = new String(outputBinary);
        Assert.assertTrue(value.startsWith("PNG"));
        // save image, user can compare the save image and original image
        String destString = "./target/binary/image" + i + ".jpg";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
        bos.write(outputBinary);
        bos.close();
        i++;
    }
    System.out.println("\nFinished");
    reader.close();

    CarbonReader reader2 = CarbonReader.builder(path, "_temp").build();

    System.out.println("\nData:");
    i = 0;
    while (i < 20 && reader2.hasNext()) {
        Object[] row = (Object[]) reader2.readNextRow();

        byte[] outputBinary = Hex.decodeHex(new String((byte[]) row[1]).toCharArray());
        System.out.println(row[0] + " " + row[2] + " image size:" + outputBinary.length);

        // validate output binary data and origin binary data
        assert (originBinary.length == outputBinary.length);
        for (int j = 0; j < originBinary.length; j++) {
            assert (originBinary[j] == outputBinary[j]);
        }

        // save image, user can compare the save image and original image
        String destString = "./target/binary/image" + i + ".jpg";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
        bos.write(outputBinary);
        bos.close();
        i++;
    }
    reader2.close();
    try {
        FileUtils.deleteDirectory(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("\nFinished");
}

From source file:org.apache.crunch.io.hbase.HFileOutputFormatForCrunch.java

@Override
public RecordWriter<Object, Cell> getRecordWriter(final TaskAttemptContext context)
        throws IOException, InterruptedException {
    Path outputPath = getDefaultWorkFile(context, "");
    Configuration conf = context.getConfiguration();
    FileSystem fs = outputPath.getFileSystem(conf);

    final boolean compactionExclude = conf.getBoolean(COMPACTION_EXCLUDE_CONF_KEY, false);

    String hcolStr = conf.get(HCOLUMN_DESCRIPTOR_KEY);
    if (hcolStr == null) {
        throw new AssertionError(HCOLUMN_DESCRIPTOR_KEY + " is not set in conf");
    }//from   w ww .j  a  va 2  s  .com
    byte[] hcolBytes;
    try {
        hcolBytes = Hex.decodeHex(hcolStr.toCharArray());
    } catch (DecoderException e) {
        throw new AssertionError("Bad hex string: " + hcolStr);
    }
    HColumnDescriptor hcol = new HColumnDescriptor();
    hcol.readFields(new DataInputStream(new ByteArrayInputStream(hcolBytes)));
    LOG.info("Output path: {}", outputPath);
    LOG.info("HColumnDescriptor: {}", hcol.toString());
    final HFile.Writer writer = HFile.getWriterFactoryNoCache(conf).withPath(fs, outputPath)
            .withComparator(KeyValue.COMPARATOR).withFileContext(getContext(hcol)).create();

    return new RecordWriter<Object, Cell>() {
        @Override
        public void write(Object row, Cell cell) throws IOException {
            KeyValue copy = KeyValue.cloneAndAddTags(cell, ImmutableList.<Tag>of());
            if (copy.getTimestamp() == HConstants.LATEST_TIMESTAMP) {
                copy.updateLatestStamp(now);
            }
            writer.append(copy);
            trt.includeTimestamp(copy);
        }

        @Override
        public void close(TaskAttemptContext c) throws IOException {
            writer.appendFileInfo(StoreFile.BULKLOAD_TIME_KEY, Bytes.toBytes(System.currentTimeMillis()));
            writer.appendFileInfo(StoreFile.BULKLOAD_TASK_KEY,
                    Bytes.toBytes(context.getTaskAttemptID().toString()));
            writer.appendFileInfo(StoreFile.MAJOR_COMPACTION_KEY, Bytes.toBytes(true));
            writer.appendFileInfo(StoreFile.EXCLUDE_FROM_MINOR_COMPACTION_KEY,
                    Bytes.toBytes(compactionExclude));
            writer.appendFileInfo(StoreFile.TIMERANGE_KEY, WritableUtils.toByteArray(trt));
            writer.close();
        }
    };
}

From source file:org.apache.ftpserver.clienttests.DecoderTest.java

public DecoderTest() throws DecoderException {
    b = Hex.decodeHex(dump.toCharArray());
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.java

public static DelegationKey delegationKeyFromXml(Stanza st) throws InvalidXmlException {
    int keyId = Integer.parseInt(st.getValue("KEY_ID"));
    long expiryDate = Long.parseLong(st.getValue("EXPIRY_DATE"));
    byte key[] = null;
    try {//from w  ww. jav a  2 s .  co  m
        key = Hex.decodeHex(st.getValue("KEY").toCharArray());
    } catch (DecoderException e) {
        throw new InvalidXmlException(e.toString());
    } catch (InvalidXmlException e) {
    }
    return new DelegationKey(keyId, expiryDate, key);
}

From source file:org.apache.hadoop.io.crypto.aes.AESCodec.java

private void createDefaultContext() {
    logger.debug("create Default Context");
    try {//from w w w.  j  ava 2s. co m
        Key key;
        String sHexKey = conf.get(CRYPTO_SECRET_KEY, null);

        if (sHexKey == null) {
            String keyName = conf.get(CRYPTO_SECRET_KEYNAME, null);
            String strKeyManagerUrl = conf.get(CRYPTO_SECRET_KEY_MANAGER_URL, null);
            if (keyName == null || strKeyManagerUrl == null) {
                throw new Exception("Both " + CRYPTO_SECRET_KEYNAME + " and " + CRYPTO_SECRET_KEY_MANAGER_URL
                        + " must be set in Configuration");
            }

            // JobConf jobConf = (JobConf)conf;
            JobConf jobConf = new JobConf(conf);
            Credentials credential = jobConf.getCredentials();
            byte[] tokenData = credential.getSecretKey(new Text(SaslUtil.KEY_TOKEN_ALIAS));
            // logger.debug("tokenData = " + new String(tokenData));

            String parameterString;
            if (jobConf.getJobName().isEmpty()) {
                // Not run in MR: To support hadoop fs -text xxx
                parameterString = TokenKeyProvider.genParameterString(strKeyManagerUrl, null);
            } else {
                if (tokenData == null)
                    throw new Exception("Fail to get Crypto SASL Token from Job Credential"
                            + "(You can create the token by bee-cli.sh and specify it by -Dmapreduce.job.credentials.binary )");
                else
                    parameterString = TokenKeyProvider.genParameterString(strKeyManagerUrl,
                            new String(tokenData));
            }
            // logger.debug("parameterString = " + parameterString);

            TokenKeyProvider keyProvider = new TokenKeyProvider();
            keyProvider.init(parameterString);
            String[] keyNames = { keyName };
            Key[] keys = keyProvider.getKeys(keyNames);
            if (keys == null || keys.length == 0)
                throw new Exception("Fail get Hex Key by key name: " + keyName);

            key = keyProvider.getKeys(keyNames)[0];
        } else {
            byte[] rawKey = Hex.decodeHex(sHexKey.toCharArray());
            int cryptographicLength = 256;
            if (rawKey.length == 16)
                cryptographicLength = 128;
            else if (rawKey.length == 32)
                cryptographicLength = 256;
            else
                throw new Exception("Invalid key length(only support 16(128)/32(256) bytes): rawKey.length="
                        + rawKey.length);
            // Key key = new Key (keyType, algorithm, 0, format, rawKey);
            key = new Key(Key.KeyType.SYMMETRIC_KEY, "AES", cryptographicLength, "RAW", rawKey);

        }

        if (cryptoContext != null)
            logger.warn("createDefaultContext: Reinit crypto context!");

        cryptoContext = new CryptoContext();
        cryptoContext.setKey(key);
    } catch (Exception e) {
        logger.error("Create Default Context Fail: " + e.getMessage(), e);
        // Ugly! BUT we want to see the Stack Trace on console
        throw new RuntimeException(
                "Create Default Context Fail(" + e.getMessage() + "): " + BeeUtil.getStackTrace(e));
    }

}