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

public static char[] encodeHex(byte[] data) 

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:com.addthis.hydra.data.filter.value.ValueFilterHash.java

@Override
public ValueObject filterValue(ValueObject value) {
    if (value == null) {
        return value;
    }/*w w w.ja  v  a  2 s. co  m*/
    long hash = 0;
    String sv = ValueUtil.asNativeString(value);
    switch (type) {
    case 0:
        hash = sv.hashCode();
        break;
    case 1:
        hash = PluggableHashFunction.hash(sv);
        break;
    case 2:
        hash = cuidHash(sv);
        break;
    case 3:
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(LessBytes.toBytes(sv));
            byte[] b = md.digest();
            for (int i = 0; i < b.length && i < 8; i++) {
                hash = (hash << 8) | (b[i] & 0xff);
            }
        } catch (NoSuchAlgorithmException e) {
            // ignore
        }
        break;
    case 4:
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.reset();
            md.update(LessBytes.toBytes(sv));
            return ValueFactory.create(new String(Hex.encodeHex(md.digest())));
        } catch (NoSuchAlgorithmException e) {
            // ignore
        }
    default:
        throw new RuntimeException("Unknown hash type: " + type);
    }
    if (abs) {
        hash = Math.abs(hash);
    }
    return ValueFactory.create(hash);
}

From source file:com.github.aelstad.keccakj.keyak.KeyakTestUtils.java

public static String toHex(byte[] buf) {
    if (buf == null)
        return "";
    return new String(Hex.encodeHex(buf));
}

From source file:com.jameslandrum.bluetoothsmart.actions.SetCharacteristic.java

@Override
public String toString() {
    return "Setting Characteristic " + mCharacteristic.getCharacteristicLabel() + " to "
            + new String(Hex.encodeHex(mData));
}

From source file:com.youTransactor.uCube.Tools.java

/**
 * function of transform the byteArray to Hexadecimal String
 * @param bytes wanted to be transform/*w ww .  j  av a  2s. c  om*/
 * @return String of HexString
 */
public static String bytesToHex(byte[] bytes) {
    return bytes == null || bytes.length == 0 ? "" : new String(Hex.encodeHex(bytes)).toUpperCase();
}

From source file:com.zimbra.cs.account.AuthTokenKey.java

public String getEncoded() {
    return mVersion + ":" + mCreated + ":" + new String(Hex.encodeHex(mKey));
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.io.FileProtection.java

/**
 * Saves the message digest file in a format compatible with {@code md5sum}.
 * //from  w  ww  .j  av a2s.  co  m
 * @param file the file to be validated
 * @param digest the message digest
 * @throws IOException if an I/O error occurred
 */
private static void saveDigest(File file, byte[] digest) throws IOException {
    PrintStream ps = null;

    try {
        ps = new PrintStream(getDigestFile(file));

        ps.print(Hex.encodeHex(digest));
        ps.print("  ");
        ps.print(file.getPath());
    } finally {
        if (ps != null) {
            ps.close();
        }
    }
}

From source file:com.fegor.alfresco.security.crypto.Crypto.java

/**
 * Encryption configuration//from   w w w  .  ja va 2s  .com
 * 
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws NoSuchPaddingException
 * @throws InvalidParameterSpecException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws UnsupportedEncodingException
 * @throws InvalidKeyException
 */
public void configEncrypt() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException,
        UnsupportedEncodingException, InvalidKeyException {
    SecretKeyFactory factory = null;
    SecretKey tmp = null;

    salt_pos = new byte[SALT_LEN];
    SecureRandom rnd = new SecureRandom();
    rnd.nextBytes(salt_pos);

    if (logger.isDebugEnabled())
        logger.debug(this.getClass().getName() + ": [salt: " + (new String(Hex.encodeHex(salt_pos))) + "]");

    factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

    /*
     * http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files
     * .shtml
     */
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt_pos, ITERATIONS, KEYLEN_BITS);
    tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

    eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    eCipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = eCipher.getParameters();

    vector_init = params.getParameterSpec(IvParameterSpec.class).getIV();

    if (logger.isDebugEnabled())
        logger.debug(
                this.getClass().getName() + ": [vector ini: " + (new String(Hex.encodeHex(vector_init))) + "]");
}

From source file:com.ikanow.infinit.e.processing.generic.aggregation.AssociationAggregationUtils.java

private static String md5checksum(String toHash) {
    try {/* ww w  .  j  a v  a 2 s.  co  m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(toHash.getBytes(Charset.forName("UTF8")));
        byte[] digest = m.digest();
        return new String(Hex.encodeHex(digest));
    } catch (Exception ex) {
        return toHash;
    }
}

From source file:com.github.aelstad.keccakj.fips202.KeccakDigestTestUtils.java

public void runTests(List<DigestTest> tests, AbstractKeccakMessageDigest messageDigest, int digestLength)
        throws Exception {
    for (DigestTest dt : tests) {
        messageDigest.reset();//from w  w  w .j a  v  a 2s  .  co m

        if ((dt.len & 7) == 0)
            messageDigest.update(dt.msg, 0, dt.len >> 3);
        else
            messageDigest.engineUpdateBits(dt.msg, 0, dt.len);

        System.out.println("Rate is now " + new String(
                Hex.encodeHex(messageDigest.getRateBits(0, Math.min(dt.len, messageDigest.getRateBits())))));
        byte[] md = messageDigest.digest();
        System.out.println("Testing length " + dt.len + ". Got " + new String(Hex.encodeHex(md)));
        Assert.assertTrue(digestLength == dt.digest.length);
        ;
        Assert.assertTrue(digestLength == md.length);
        ;
        org.junit.Assert.assertTrue(Arrays.equals(md, dt.digest));
    }

    testPerformance(messageDigest);
}

From source file:com.spidertracks.datanucleus.query.runtime.EqualityOperand.java

/**
 * Add the index expression to the clause
 * /*from w w w  . j  a va  2s . c  o m*/
 * @param expression the expression to check.
 * @param isIndexed true if there is a secondary index on the field to check equality for.
 */
public void addExpression(final IndexExpression expression, final boolean isIndexed) {
    this.isIndexed |= isIndexed;
    this.clause.addToExpressions(expression);

    if (logger.isDebugEnabled()) {
        logger.debug("Adding clause for name: {} value: {}",
                new String(Hex.encodeHex(expression.getColumn_name())),
                new String(Hex.encodeHex(expression.getValue())));
    }
}