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

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

Introduction

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

Prototype

public Hex() 

Source Link

Document

Creates a new codec with the default charset name #DEFAULT_CHARSET_NAME

Usage

From source file:net.sourceforge.jencrypt.lib.CryptoWrapper.java

/**
 * Read the encryption key into a byte array.
 * /*from   w ww  . j  a  v  a2  s .c  o  m*/
 * @param keyFile
 *            the file with the en/decryption key
 * @return byte[]
 */
public byte[] readKeyFile(File keyFile) throws IOException {
    byte[] keyBytes = null;
    byte[] bytes = null;
    try {
        keyBytes = Utils.getBytesFromFile(keyFile);
        // convert hex to bytes
        bytes = new Hex().decode(keyBytes);
    } catch (Exception e) {
        throw new IOException("Error reading keyfile '" + keyFile.getName() + "': " + e.getMessage());
    }
    return bytes;
}

From source file:jp.primecloud.auto.api.ApiFilter.java

/**
 *
 * HMAC-SHA256???//www  .j  a va 2  s  . c  o m
 *
 * @param plainText ?
 * @param keyText
 * @return
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
private static String encodeSHA256(String plainText, String keyText)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
    SecretKey secretKey = new SecretKeySpec(keyText.getBytes("UTF-8"), "HmacSHA256");
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(secretKey);

    byte[] plainBytes = plainText.getBytes("UTF-8");
    byte[] encodedBytes = mac.doFinal(plainBytes);
    byte[] hexBytes = new Hex().encode(encodedBytes);

    return new String(hexBytes, "UTF-8");
}

From source file:com.baidu.cc.configuration.service.impl.VersionServiceImpl.java

/**
 * Write json string to {@link OutputStream}.
 * /*from   w  w  w.  ja  v  a 2s  .  c o m*/
 * @param jsonArray
 *            {@link JsonArray}
 * @param os
 *            {@link OutputStream}
 * @throws IOException
 *             ??
 */
private void writeJson(JsonArray jsonArray, OutputStream os) throws IOException {
    String json = gson.toJson(jsonArray);
    // to byte array
    byte[] byteArray = json.getBytes(SysUtils.UTF_8);

    Hex encoder = new Hex();
    byteArray = encoder.encode(byteArray);
    IOUtils.write(byteArray, os);
}

From source file:TimestreamsTests.java

/**
 * Generate an HMAC Based on example:// ww  w  . ja v  a2 s .c o  m
 * http://stackoverflow.com/questions/6312544
 * /hmac-sha1-how-to-do-it-properly-in-java
 * 
 * @param value
 *            is a String to hash
 * @param key
 *            is the private key to hash with#
 * @param type
 *            is the Mac format to use such as HmacSHA256
 * @return The hmac
 */
private String hmacString(String value, String key, String type) {
    try {
        byte[] keyBytes = key.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, type);

        // Get a Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(type);
        mac.init(signingKey);

        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(value.getBytes());

        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);

        // Covert array of Hex bytes to a String
        return new String(hexBytes, "UTF-8");
    } catch (Exception e) {
        fail = e.getLocalizedMessage();
        return null;
    }
}

From source file:com.cloudant.sync.replication.BasicPushStrategy.java

public String getReplicationId() {
    HashMap<String, String> dict = new HashMap<String, String>();
    dict.put("source", this.sourceDb.getIdentifier());
    dict.put("target", this.targetDb.getIdentifier());
    // get raw SHA-1 of dictionary
    byte[] sha1Bytes = Misc.getSha1(new ByteArrayInputStream(JSONUtils.serializeAsBytes(dict)));
    // return SHA-1 as a hex string
    byte[] sha1Hex = new Hex().encode(sha1Bytes);
    return new String(sha1Hex);
}

From source file:com.diversityarrays.dalclient.DalUtil.java

/**
 * Calculate an RFC 2104 compliant HMAC signature.
 * @param key is the signing key/* ww w . ja va2  s.  co m*/
 * @param data is the data to be signed
 * @return the base64-encoded signature as a String
 */
public static String computeHmacSHA1(String key, String data) {
    try {
        byte[] keyBytes = key.getBytes(cryptCharsetName);
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, ALGORITHM_HMAC_SHA1);

        Mac mac = Mac.getInstance(ALGORITHM_HMAC_SHA1);
        mac.init(signingKey);

        byte[] rawHmac = mac.doFinal(data.getBytes(cryptCharsetName));

        byte[] hexBytes = new Hex().encode(rawHmac);

        return new String(hexBytes, cryptCharsetName);

    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cloudant.sync.datastore.AttachmentManager.java

private String keyToString(byte[] key) {
    return new String(new Hex().encode(key));
}

From source file:com.linuxbox.enkive.docstore.AbstractDocStoreService.java

/**
 * Returns a string representation of an array of bytes.
 * /*from   ww w .j  a v a  2s.  c o  m*/
 * @param hash
 * @return
 */
public static String getIdentifierFromHash(byte[] hash) {
    return new String((new Hex()).encode(hash));
}

From source file:com.baidu.cc.ConfigLoader.java

/**
 * Read property from local resource file
 * /*  w ww.  j a v  a  2  s  .c  om*/
 * @param props to merge from local
 * @param localFile local resource file
 * @throws IOException throw all file operation exception
 */
public void readLocal(Properties props, File localFile) throws IOException {
    Assert.notNull(localFile, "Property 'localFile' is null.");
    if (!localFile.exists()) {
        throw new IOException("File not exist. " + localFile.getPath());
    }

    byte[] byteArray = FileUtils.readFileToByteArray(localFile);
    Hex encoder = new Hex();
    try {
        byteArray = encoder.decode(byteArray);
    } catch (DecoderException e) {
        throw new IOException(e.getMessage());
    }

    String json = new String(byteArray, FILE_ENCODE);

    Map<String, String> map = gson.fromJson(json, new TypeToken<Map<String, String>>() {
    }.getType());

    setVersionTag(map);
    props.putAll(map);
}

From source file:com.baidu.cc.ConfigLoader.java

/**
 * Write property content to local resource file.
 * //from   ww  w  . j  a  v  a  2s. c o m
 * @param configItems latest configuration items
 * @param localFile loca resource file
 * @throws IOException throw all file operation exception
 */
public void writeLocal(Map<String, String> configItems, File localFile) throws IOException {
    Assert.notNull(localFile, "Property 'localFile' is null.");
    if (!localFile.exists()) {
        throw new IOException("File not exist. " + localFile.getPath());
    }

    String json = gson.toJson(configItems);
    //to byte array
    byte[] byteArray = json.getBytes(FILE_ENCODE);

    Hex encoder = new Hex();
    byteArray = encoder.encode(byteArray);

    FileUtils.writeByteArrayToFile(localFile, byteArray);
}