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

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

Introduction

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

Prototype

protected static char[] encodeHex(byte[] data, char[] toDigits) 

Source Link

Document

Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.

Usage

From source file:de.alpharogroup.crypto.hex.HexableEncryptor.java

/**
 * {@inheritDoc}/*www  . jav  a  2  s .  co m*/
 *
 * @throws InvalidKeyException
 *             the invalid key exception is thrown if initialization of the cypher object fails.
 * @throws UnsupportedEncodingException
 *             is thrown by get the byte array of the private key String object fails or if the
 *             named charset is not supported.
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the cypher object fails.
 * @throws NoSuchPaddingException
 *             is thrown if instantiation of the cypher object fails.
 * @throws IllegalBlockSizeException
 *             is thrown if {@link Cipher#doFinal(byte[])} fails.
 * @throws BadPaddingException
 *             is thrown if {@link Cipher#doFinal(byte[])} fails.
 */
@Override
public String encrypt(final String string) throws InvalidKeyException, UnsupportedEncodingException,
        NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    final byte[] utf8 = string.getBytes("UTF-8");
    final byte[] encrypt = getModel().getCipher().doFinal(utf8);
    final char[] original = Hex.encodeHex(encrypt, false);
    return new String(original);
}

From source file:com.chiorichan.util.ObjectUtil.java

public static String hex2Readable(byte... elements) {
    // TODO Char Dump
    String result = "";
    char[] chars = Hex.encodeHex(elements, true);
    for (int i = 0; i < chars.length; i = i + 2)
        result += " " + chars[i] + chars[i + 1];

    if (result.length() > 0)
        result = result.substring(1);//from  w ww. j  av a  2s  . c o  m

    return result;
}

From source file:com.sap.core.odata.core.edm.EdmBinary.java

@Override
public String toUriLiteral(final String literal) throws EdmSimpleTypeException {
    return "binary'" + String.valueOf(Hex.encodeHex(Base64.decodeBase64(literal), false)) + "'";
}

From source file:com.isa.ws.utiles.UtilesSWHelper.java

public static String convertBase64ToHexa(String base64) {
    byte[] decodeBase64 = convertBase64ToBytes(base64);
    char[] decodedHex = Hex.encodeHex(decodeBase64, false);
    return new String(decodedHex);
}

From source file:eu.europa.esig.dss.DSSUtils.java

/**
 * Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned
 * String will be double the length of the passed array, as it takes two characters to represent any given byte. If
 * the input array is null then null is returned. The obtained string is converted to uppercase.
 *
 * @param value/*from   w w w  .j a v a2  s. c o  m*/
 * @return
 */
public static String toHex(final byte[] value) {
    return (value != null) ? new String(Hex.encodeHex(value, false)) : null;
}

From source file:com.mobilehelix.appserver.push.PushManager.java

private String getUniqueID(String clientid, String userid, Long appID)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    digest.reset();/*from   w  ww .  j  av  a  2  s . c  o m*/
    digest.update(clientid.getBytes("utf8"));
    digest.update(userid.getBytes("utf8"));
    digest.update(appID.toString().getBytes("utf8"));

    // Add in a random 8 bytes salt.
    byte saltb[] = new byte[8];
    srandom.nextBytes(saltb);
    digest.update(saltb);

    byte[] res = digest.digest();
    return new String(Hex.encodeHex(res, true));
}

From source file:com.tesora.dve.sql.schema.PETable.java

public static String buildTypeHash(String in) {
    try {/*  w w w  . j  av  a 2s.co  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] bout = md.digest(PECharsetUtils.getBytes(in, PECharsetUtils.UTF_8));
        String out = new String(Hex.encodeHex(bout, true));
        return out;
    } catch (NoSuchAlgorithmException nsae) {
        throw new SchemaException(Pass.PLANNER, "Unable to obtain sha-1 hash of type definition");
    }
}

From source file:de.acosix.alfresco.mtsupport.repo.sync.PersonWorkerImpl.java

protected boolean checkForDigestDifferences(final AvatarBlobWrapper avatarWrapper,
        final NodeRef preferenceImage) {
    String newImageDigestHexStr = null;
    String existingImageDigestHexStr = null;

    try {/* ww  w  . ja  v a  2  s .c o  m*/
        final MessageDigest newImageMD = MessageDigest.getInstance("MD5");
        newImageMD.update(avatarWrapper.getData());
        final byte[] newImageDigest = newImageMD.digest();
        final char[] newImageDigestHex = Hex.encodeHex(newImageDigest, false);
        newImageDigestHexStr = new String(newImageDigestHex);

        final MessageDigest existingImageMD = MessageDigest.getInstance("MD5");
        final ContentReader existingImageReader = this.contentService.getReader(preferenceImage,
                ContentModel.PROP_CONTENT);
        try (InputStream contentInputStream = existingImageReader.getContentInputStream()) {
            final byte[] buffer = new byte[1024 * 512];
            int bytesRead = -1;
            while ((bytesRead = contentInputStream.read(buffer)) != -1) {
                existingImageMD.update(buffer, 0, bytesRead);
            }

            final byte[] existimageDigest = existingImageMD.digest();
            final char[] existingImageDigestHex = Hex.encodeHex(existimageDigest, false);
            existingImageDigestHexStr = new String(existingImageDigestHex);
        } catch (final IOException ioex) {
            LOGGER.warn("Error creating digest from existing person avatar", ioex);
        }
    } catch (final NoSuchAlgorithmException dex) {
        LOGGER.warn("Error creating digest for new/existing person avatar", dex);
    }

    final boolean difference = !EqualsHelper.nullSafeEquals(newImageDigestHexStr, existingImageDigestHexStr,
            true);
    return difference;
}

From source file:com.bittorrent.mpetazzoni.common.Torrent.java

/**
 * Convert a byte string to a string containing an hexadecimal
 * representation of the original data.//  w  w w  .  j ava 2  s .  c o m
 *
 * @param bytes The byte array to convert.
 */
public static String byteArrayToHexString(byte[] bytes) {
    return new String(Hex.encodeHex(bytes, false));
}

From source file:net.officefloor.plugin.web.http.security.scheme.DigestHttpSecuritySourceTest.java

/**
 * Creates the digest./*from w  w  w  .j  ava  2 s  .  com*/
 * 
 * @param algorithm
 *            Algorithm.
 * @param values
 *            Values to load into the digest.
 * @return Digest.
 */
private byte[] createDigest(String algorithm, String... values) {
    try {

        // Obtain the charset
        Charset usAscii = HttpRequestParserImpl.US_ASCII;

        // Create the digest
        MessageDigest message = MessageDigest.getInstance(algorithm);
        for (int i = 0; i < values.length; i++) {
            String value = values[i];
            if (i > 0) {
                message.update(":".getBytes(usAscii));
            }
            message.update(value.getBytes(usAscii));
        }
        byte[] digest = message.digest();

        // Transform to text
        String text = new String(Hex.encodeHex(digest, true));

        // Return the digest
        return text.getBytes(usAscii);

    } catch (Exception ex) {
        throw fail(ex);
    }
}