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.pinterest.pinlater.backends.common.PinLaterBackendUtils.java

/**
 * Get the salted hash of a password.//from ww w .  j  a va2  s .c  om
 *
 * @param password  String to be salted and hashed.
 * @return Salted hash of the password string.
 */
public static String getSaltedHash(String password) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    String strToDigest = password + SALT;
    byte[] digestedStr = digest.digest(strToDigest.getBytes());
    return new String(Hex.encodeHex(digestedStr));
}

From source file:com.ery.ertc.estorm.util.MD5Hash.java

/**
 * Given a byte array, returns its MD5 hash as a hex string. Only "length" number of bytes starting at "offset" within the byte array
 * are used./*from w  ww.ja va2  s. co m*/
 * 
 * @param key
 *            the key to hash (variable length byte array)
 * @param offset
 * @param length
 * @return MD5 hash as a 32 character hex string.
 */
public static String getMD5AsHex(byte[] key, int offset, int length) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(key, offset, length);
        byte[] digest = md.digest();
        return new String(Hex.encodeHex(digest));
    } catch (NoSuchAlgorithmException e) {
        // this should never happen unless the JDK is messed up.
        throw new RuntimeException("Error computing MD5 hash", e);
    }
}

From source file:cherry.foundation.crypto.SecureTypeEncoder.java

@Override
public String encode(T p) {
    byte[] pln = typeToBytes(p);
    byte[] crp = crypto.encrypt(pln);
    return new String(Hex.encodeHex(crp));
}

From source file:com.medsphere.ovid.common.uuid.KeyGenerator.java

public KeyGenerator() {
    try {/*w  w w.j a va2  s . c o  m*/
        InetAddress inet = InetAddress.getLocalHost();
        byte[] bytes = inet.getAddress();
        String inetAddressInHex = new String(Hex.encodeHex(bytes));
        String hashCodeForThis = new String(Hex.encodeHex(intToByteArray(System.identityHashCode(this))));
        middle = inetAddressInHex + hashCodeForThis;
        seeder = new SecureRandom();
        seeder.nextInt();

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        seeder = new SecureRandom();
        seeder.nextInt();
        middle = new Integer(this.hashCode()).toString();
    }

}

From source file:jp.co.opentone.bsol.framework.core.util.HashUtil.java

/**
 * ????????????.//from w w  w .j  av  a  2  s. c  o  m
 * <h3>?</h3>
 * <pre>
 * 1. 3??
 * 2. ???(ab, 01, ...)?
 * 3. ALGORITHM?????
 *</pre>
 * @param   parameter ?
 * @return  ?
 */
public static String getString(String parameter) {
    String result = null;

    if (parameter != null) {
        String newParameter = parameter + parameter + parameter;
        byte[] encodedParameter = newParameter.getBytes();
        for (int i = 0; i < encodedParameter.length; i++) {
            encodedParameter[i] = (byte) (encodedParameter[i] + 1);
        }
        try {
            MessageDigest md = MessageDigest.getInstance(ALGORITHM);
            md.update(encodedParameter);
            result = new String(Hex.encodeHex(md.digest()));
        } catch (NoSuchAlgorithmException e) {
            // ALGORITHM?????????????
            throw new RuntimeException(e);
        }
    }
    return result;
}

From source file:de.estudent.nfc.NFCHelper.java

public static String encodeToHexString(byte b) {
    byte[] array = new byte[1];
    array[0] = b;/*  w  w  w  . j  a  va2 s .  c o  m*/
    String result = new String(Hex.encodeHex(array));
    result = "0x" + result.toUpperCase();
    return result;
}

From source file:edu.harvard.med.screensaver.util.CryptoUtils.java

/**
 * Utility method for generating a SHA-digested (hashed) version of a byte[].
 * // ww w .java2s  .  c om
 * @param bytes
 *          array of bytes to be hashed
 * @return the digested (hashed) version of the byte[], as a hex String.
 */
public static String digest(byte[] bytes) {
    try {
        byte[] resultBytes = MessageDigest.getInstance("SHA").digest(bytes);
        char[] resultHexChars = Hex.encodeHex(resultBytes);
        return new String(resultHexChars);
    } catch (Exception e) {
        e.printStackTrace();
        log.error("error trying to digest bytes: " + e.getMessage());
        return null;
    }
}

From source file:io.manasobi.utils.CryptoUtils.java

/**
 * ?   ?. ??  ?  ??  ?  ?./*from ww  w . j  a va  2  s  .c o m*/
 *
 * @return ?? ??  Hex ?  
 */
public static String generateHexDESKey() {

    byte[] rawKey = null;

    try {

        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);

        SecretKey secretKey = keyGenerator.generateKey();

        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);

        DESedeKeySpec desEdeSpec = (DESedeKeySpec) secretKeyFactory.getKeySpec(secretKey,
                javax.crypto.spec.DESedeKeySpec.class);

        rawKey = desEdeSpec.getKey();

    } catch (Exception e) {

        throw new CryptoUtilsException(e.getMessage());
    }

    return new String(Hex.encodeHex(rawKey));
}

From source file:com.aqnote.shared.cryptology.symmetric.DES.java

public synchronized static String encrypt(String plaintext) {
    try {/*from  w  w w  .  ja v  a  2s  .c  o  m*/
        if (plaintext == null) {
            return null;
        }
        return new String(Hex.encodeHex(encodeCipher.doFinal(plaintext.getBytes(ENCODE_UTF_8))));
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.aqnote.shared.cryptology.symmetric.AES.java

public static String encrypt(String plaintext) throws RuntimeException {
    try {//from  w  w  w  . j  ava 2 s .  c o  m
        if (plaintext == null) {
            return null;
        }
        return new String(Hex.encodeHex(encodeCipher.doFinal(plaintext.getBytes(ENCODE_UTF_8))));
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}