Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

In this page you can find the example usage for java.security MessageDigest digest.

Prototype

public byte[] digest() 

Source Link

Document

Completes the hash computation by performing final operations such as padding.

Usage

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

/**
 * ????????????./*from  w w w .j  a v  a  2s  . com*/
 * <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:Main.java

private static byte[] generateKey(String strSrc) {

    MessageDigest digest;

    byte[] keyBytes = new byte[32];
    try {/* w ww  . j a v  a2 s. c  om*/
        digest = MessageDigest.getInstance("SHA-256");
        digest.update(strSrc.getBytes("UTF-8"));
        System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return keyBytes;
}

From source file:Main.java

/**
 * Calculates an MD5 hash for a text./*from ww w .ja  v  a 2  s  .c  o  m*/
 * 
 * @param dataToEncode
 *            The data to encode.
 * @return A text representation of a hexadecimal value of length 32.
 */
public static String messageDigestFive(final String dataToEncode) {
    MessageDigest m;
    try {
        m = MessageDigest.getInstance("MD5");
        byte[] data = dataToEncode.getBytes();
        m.update(data, 0, data.length);
        BigInteger i = new BigInteger(1, m.digest());
        return String.format("%1$032X", i);
    } catch (NoSuchAlgorithmException e) {
        // MD5 Should be supported by the runtime!
        throw new IllegalStateException(e);
    }
}

From source file:it.zero11.acme.utils.JWKUtils.java

private static byte[] SHA256(String text) {
    try {/*from   ww  w .  j  a  va2 s  .c om*/
        MessageDigest md;
        md = MessageDigest.getInstance("SHA-256");
        md.update(text.getBytes("UTF-8"), 0, text.length());
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.incrementalism.tooter.User.java

private static String hash(String password) {
    // NOTE: Insecure! Don't use this in a real application
    try {//from   w w  w .  j  av a2  s  .  com
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(password.getBytes("UTF-8"));
        return Base64.encodeBase64String(md.digest());
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError("No SHA");
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError("No UTF-8");
    }
}

From source file:Main.java

public static String sha1(String text) {
    MessageDigest md;
    try {/*from www  .  j av a2 s  .c om*/
        md = MessageDigest.getInstance("SHA-1");
        byte[] sha1hash = new byte[40];
        md.update(text.getBytes("UTF-8"), 0, text.length());
        sha1hash = md.digest();
        return convertToHex(sha1hash);
    } catch (Exception e) {
    }

    return "";
}

From source file:me.figo.internal.FigoTrustManager.java

private static String getThumbPrint(X509Certificate cert) {
    try {//from  ww w  . j  av  a 2s  .c o  m
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] der = cert.getEncoded();
        md.update(der);
        byte[] digest = md.digest();
        return new String(Hex.encodeHex(digest, false));
    } catch (NoSuchAlgorithmException e) {
        return "";
    } catch (CertificateEncodingException e) {
        return "";
    }
}

From source file:Main.java

/**
 * Calculation md5 hash of string//from   ww  w .ja v a  2  s  . c om
 * 
 * @param s - string for hash calculation
 * @return md5 hash of input string
 */
public static String MD5Hash(String s) {
    int sHash = s.hashCode();

    String result = md5Cache.get(sHash);

    if (result != null)
        return result;

    try {
        if (MD5 == null)
            MD5 = MessageDigest.getInstance("MD5");

        MessageDigest alg = (MessageDigest) MD5.clone();
        alg.update(s.getBytes());

        StringBuffer hexString = new StringBuffer(32);

        for (byte b : alg.digest())
            hexString.append(intToHexChars(0xFF & b));

        result = hexString.toString();
    } catch (Exception e) {
    }

    if (result == null)
        result = String.valueOf(s.hashCode());
    md5Cache.put(sHash, result);

    return result;
}

From source file:Main.java

/**
 * Generates SHA256 hash of the password which is used as key
 *
 * @param password used to generated key
 * @return SHA256 of the password//from  w w  w .j  av a 2  s  .c om
 */
private static SecretKeySpec generateKey(final String password)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    final MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
    byte[] bytes = password.getBytes("UTF-8");
    digest.update(bytes, 0, bytes.length);
    byte[] key = digest.digest();

    log("SHA-256 key ", key);

    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    return secretKeySpec;
}

From source file:org.confab.Utilities.java

public static String md5(String pass) {
    String md5 = "";
    try {/*ww w .  j a v  a 2 s.com*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] data = pass.getBytes();
        m.update(data, 0, data.length);
        BigInteger i = new BigInteger(1, m.digest());
        md5 = String.format("%1$032x", i);
    } catch (NoSuchAlgorithmException e) {
        System.out.println(e);
    }
    return md5;
}