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:Main.java

public static String md5Encoder(String content, boolean isCapital) {
    try {/*from   w w w  . j  a  va  2 s. c  o m*/
        byte[] strTemp = content.getBytes();
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        byte[] mds = mdTemp.digest();
        int j = mds.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (byte md : mds) {
            if (isCapital) {
                str[k++] = CAPITAL_HEX_DIGITS[md >> 4 & 0xf];
                str[k++] = CAPITAL_HEX_DIGITS[md & 0xf];
            } else {
                str[k++] = LOWER_HEX_DIGITS[md >> 4 & 0xf];
                str[k++] = LOWER_HEX_DIGITS[md & 0xf];
            }
        }
        return new String(str);
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static synchronized String md5(MessageDigest md5) {
    StringBuffer strBuf = new StringBuffer();
    byte[] result16 = md5.digest();
    char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    for (int i = 0; i < result16.length; i++) {
        char[] c = new char[2];
        c[0] = digit[result16[i] >>> 4 & 0x0f];
        c[1] = digit[result16[i] & 0x0f];
        strBuf.append(c);//from w ww  . j av  a 2 s  .  c o  m
    }

    return strBuf.toString();
}

From source file:Main.java

private static String hashAlgorithm(String hash, String text)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance(hash);
    md.update(text.getBytes("iso-8859-1"), 0, text.length());

    return convertToHex(md.digest());
}

From source file:Main.java

public static final String hash(final String s, final String algorithm) {
    try {//  w w  w .ja  v  a2 s  . c o m
        // Create MD5 or SHA-256 Hash
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String generateSHA1Hash(String textToBeHashed) {
    try {/*  ww  w . jav  a  2  s  .  c  o  m*/
        MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
        messageDigest.update(textToBeHashed.getBytes());
        byte[] sha1hash = messageDigest.digest();
        return getHexString(sha1hash);
    } catch (Exception e) {
        throw new RuntimeException("Couldn't generate SHA-1 hash for " + textToBeHashed);
    }
}

From source file:Main.java

public static String md5(String str) {
    String cacheKey;//w w  w.  j a va2 s  . c  om
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(str.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(str.hashCode());
    }
    return cacheKey;
}

From source file:Main.java

public static String keyToHashKey(String key) {
    String cacheKey;//from   w  w  w . j a v  a  2s.  c om
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(key.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}

From source file:Main.java

public static String makeMD5Hash(byte[] bytes) {
    try {/*w  w  w  .j  av  a  2  s  .  c  om*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(bytes, 0, bytes.length);
        byte[] sha1hash = md.digest();
        return convertToHex(sha1hash);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:cc.recommenders.utils.Fingerprints.java

private static String toString(final MessageDigest digest) {
    final byte[] res = digest.digest();
    return toHexString(res);
}

From source file:Main.java

public static String generateHash(String pText) throws Exception {
    String hashValue;// w  ww  . ja  va 2 s  . c  o  m
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(pText.getBytes("ASCII"));
    hashValue = encodeHex(md.digest());
    return hashValue;
}