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:Main.java

public static List<byte[]> hexToListBytesArray(String s) throws DecoderException {
    List<String> listHex = new ArrayList<String>();
    if (s.length() == 2) {
        listHex.add(s);//  w  ww .  j  a  v a2 s.c  om
    } else if (s.length() > 2 && ((s.length() % 2) == 0)) {
        for (int i = 0; i < s.length(); i++) {
            String ss = s.substring(i, i + 2);
            i = i + 1;
            listHex.add(ss);
        }
    } else {
        return null;
    }
    Hex hex = new Hex();

    List<byte[]> listByteArray = new ArrayList<byte[]>();
    for (String sh : listHex) {
        listByteArray.add(hex.decode(sh.getBytes()));
    }
    return listByteArray;
}

From source file:me.buom.shiro.util.HmacSha1.java

public static byte[] hash(byte[] privateKey, String stringToSign)
        throws NoSuchAlgorithmException, InvalidKeyException {
    // Get an hmac_sha1 key from the raw key bytes
    SecretKeySpec signingKey = new SecretKeySpec(privateKey, HMAC_SHA1_ALGORITHM);

    // Get an hmac_sha1 Mac instance and initialize with the signing key
    Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    mac.init(signingKey);// www. j ava  2 s. c  o m

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

    // Convert raw bytes to Hex
    return new Hex().encode(rawHmac);
}

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

/**
 * Convert a hex byte array back to a String
 *
 * @param bytes The hex byte array//w ww . j a va 2s.  c  o  m
 * @return The string
 */
public static final String byteArrayToHexString(byte[] bytes) {
    return new String(new Hex().encode(bytes), Charset.forName("UTF-8"));
}

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

@Test
public void toStringTest() throws Exception {
    final Hex hex = new Hex();
    final String str = "DEADBEEF";
    final HexWritable hw = new HexWritable(hex.decode(str.getBytes()));
    assertEquals(str, hw.toString().toUpperCase());
}

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

@Test
public void toStringTest() throws Exception {
    final Hex hex = new Hex();
    final String str = "DEADBEEF";
    final ImmutableHexWritable ihw = new ImmutableHexWritable(hex.decode(str.getBytes()));
    assertEquals(str, ihw.toString().toUpperCase());
}

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

@Test
public void makeOutKeyMD5() throws Exception {
    final Hex hex = new Hex();
    final byte[] hash = hex.decode("8a9111fe05f9815fc55c728137c5b389".getBytes());
    final byte type = 0;
    final byte[] col = "vassalengine.org/VASSAL 3.1.15".getBytes();

    // It's neat that I have to cast one-byte numeric literals to bytes.
    // Thanks, Java!
    final byte[] expected = { (byte) 0x8a, (byte) 0x91, (byte) 0x11, (byte) 0xfe, // MD5
            (byte) 0x05, (byte) 0xf9, (byte) 0x81, (byte) 0x5f, (byte) 0xc5, (byte) 0x5c, (byte) 0x72,
            (byte) 0x81, (byte) 0x37, (byte) 0xc5, (byte) 0xb3, (byte) 0x89, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x00, // padding
            (byte) 0x00, // type
            'v', 'a', 's', 's', 'a', 'l', 'e', 'n', 'g', // column
            'i', 'n', 'e', '.', 'o', 'r', 'g', '/', 'V', 'A', 'S', 'S', 'A', 'L', ' ', '3', '.', '1', '.', '1',
            '5' };

    assertArrayEquals(expected, KeyUtils.makeEntryKey(hash, type, col));
}

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

@Override
public String toString() {
    final Hex hex = new Hex();
    final byte[] bytes = new byte[getLength()];
    System.arraycopy(getBytes(), 0, bytes, 0, bytes.length);
    return new String(hex.encode(bytes));
}

From source file:com.gmu.uav.RadioSecurity.java

public static String hmacSha1(String value, byte[] key) {
    try {/*from ww w  . j  a  va2s.  c  o m*/
        // Get an hmac_sha1 key from the raw key bytes    
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacSHA1");
        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) {
        throw new RuntimeException(e);
    }
}

From source file:de.marx_labs.utilities.common.util.HashUtil.java

public static String hmacSha1(String value, String key) {
    try {//from   ww  w  .jav  a  2 s  .  co  m
        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = key.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacSHA1");
        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) {
        throw new RuntimeException(e);
    }
}

From source file:com.diversityarrays.dal.db.DalDatabaseUtil.java

/**
 * Calculate an RFC 2104 compliant HMAC signature.
 * @param key is the signing key/*ww  w  .  j  a v a  2 s.  c  o 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("UTF-8");
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");

        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);

        byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));

        // TODO Consider replacing with a simple hex encoder so we don't need commons-codec
        byte[] hexBytes = new Hex().encode(rawHmac);

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

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