Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

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

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:Main.java

public static byte[] getMessageDigest(byte[] data, String algorithm) {
    try {/*from   w w w . java 2s  .  c om*/
        final MessageDigest md = MessageDigest.getInstance(algorithm);
        final byte[] digest = md.digest(data);
        return digest;
    } catch (final NoSuchAlgorithmException e) {
        Log.e(e.getMessage(), e.getMessage());
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String keyToHashKey(String key) {
    String cacheKey;//from   w  w  w . j ava  2  s  .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 getStringMD5(String key) {
    MessageDigest md5 = null;//from www  . j  av  a 2  s.  c  o  m
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    md5.update(key.getBytes());
    //important: use Base64.URL_SAFE flag to avoid "+" and "/"
    return new String(Base64.encode(md5.digest(), Base64.URL_SAFE));
}

From source file:Main.java

public static String MD5(String s) {
    try {/*from   w  w w .ja v a  2s  .  c o m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(s.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuilder buf = new StringBuilder("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "null";
}

From source file:Main.java

public static String toMd5(String input) {
    String output = input;/* w w w. j  a v a  2  s. c  o  m*/
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(input.getBytes(), 0, input.length());
        output = new BigInteger(1, md5.digest()).toString(16);

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

From source file:Main.java

public static String calculateMD5(String string, String encoding) {
    try {// w  w w.j av  a  2s .co  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(string.getBytes(Charset.forName(encoding)));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

public static byte[] halfSHA512(byte[] bytesToHash) {
    try {//  ww  w  .  j  a  v a2  s. co m
        MessageDigest sha512Digest = MessageDigest.getInstance("SHA-512");
        byte[] bytesHash = sha512Digest.digest(bytesToHash);
        byte[] first256BitsOfHash = copyOf(bytesHash, 32);
        return first256BitsOfHash;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String md5String(String val) {
    if (val == null)
        return null;
    byte[] resBytes;
    try {//from  w ww  .  j av a 2 s. com
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(val.getBytes("UTF-8"));
        resBytes = md.digest();
        return hexString(resBytes);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static byte[] md5(byte[] bytes) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5Util");
    md.update(bytes);/*from w w w  .j  a  v a2s  .c  om*/
    return md.digest();
}

From source file:Main.java

public static String encryption(String plainText) {
    String re_md5 = "";
    try {/*from   w w  w. ja v  a 2 s  .  c  om*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();
        int i = 0;

        StringBuilder sb = new StringBuilder("");
        for (int offset = 0, len = b.length; offset < len; offset++) {
            i = b[offset];
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(i));
        }
        re_md5 = sb.toString();

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