Search in sources :

Example 1 with Hex

use of org.apache.commons.codec.binary.Hex in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class WhenEncoding method shouldConvertToHex.

@Test
public void shouldConvertToHex() throws DecoderException {
    String text = randomString();
    String actual = Encoding.HEX.encode(text.getBytes(CHAR_SET));
    assertEquals("Text", text, new String(new Hex().decode(actual.getBytes(CHAR_SET)), CHAR_SET));
}
Also used : Hex(org.apache.commons.codec.binary.Hex) Test(org.junit.Test)

Example 2 with Hex

use of org.apache.commons.codec.binary.Hex in project algoliasearch-client-java by algolia.

the class APIClient method hmac.

static String hmac(String key, String msg) {
    Mac hmac;
    try {
        hmac = Mac.getInstance("HmacSHA256");
    } catch (NoSuchAlgorithmException e) {
        throw new Error(e);
    }
    try {
        hmac.init(new SecretKeySpec(key.getBytes(), "HmacSHA256"));
    } catch (InvalidKeyException e) {
        throw new Error(e);
    }
    byte[] rawHmac = hmac.doFinal(msg.getBytes());
    byte[] hexBytes = new Hex().encode(rawHmac);
    return new String(hexBytes);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Hex(org.apache.commons.codec.binary.Hex) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 3 with Hex

use of org.apache.commons.codec.binary.Hex in project nem-library by rosklyar.

the class HexEncoder method getString.

/**
 * Converts a byte array to a hex string.
 *
 * @param bytes The input byte array.
 * @return The output hex string.
 */
public static String getString(final byte[] bytes) {
    final Hex codec = new Hex();
    final byte[] decodedBytes = codec.encode(bytes);
    return StringEncoder.getString(decodedBytes);
}
Also used : Hex(org.apache.commons.codec.binary.Hex)

Example 4 with Hex

use of org.apache.commons.codec.binary.Hex in project ninja by ninjaframework.

the class Crypto method signHmacSha1.

private String signHmacSha1(String value, String key) {
    try {
        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
        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(StandardCharsets.UTF_8));
        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);
        // Convert array of Hex bytes to a String
        return new String(hexBytes, "UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Hex(org.apache.commons.codec.binary.Hex) Mac(javax.crypto.Mac)

Example 5 with Hex

use of org.apache.commons.codec.binary.Hex in project nem-library by rosklyar.

the class HexEncoder method getBytesInternal.

private static byte[] getBytesInternal(final String hexString) throws DecoderException {
    final Hex codec = new Hex();
    final String paddedHexString = 0 == hexString.length() % 2 ? hexString : "0" + hexString;
    final byte[] encodedBytes = StringEncoder.getBytes(paddedHexString);
    return codec.decode(encodedBytes);
}
Also used : Hex(org.apache.commons.codec.binary.Hex)

Aggregations

Hex (org.apache.commons.codec.binary.Hex)8 Mac (javax.crypto.Mac)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Test (org.junit.Test)1