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 String md5(String input) {
    String res = "";
    try {/*from   w  w  w . j  a v a 2s  .c  o  m*/
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(input.getBytes());
        byte[] md5 = algorithm.digest();
        String tmp = "";
        for (int i = 0; i < md5.length; i++) {
            tmp = (Integer.toHexString(0xFF & md5[i]));
            if (tmp.length() == 1) {
                res += "0" + tmp;
            } else {
                res += tmp;
            }
        }
    } catch (NoSuchAlgorithmException ex) {
    }
    return res;
}

From source file:Main.java

public static String md5(String input) {
    try {//from   www  .  ja  v  a  2 s .  co  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(input.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte anArray : array) {
            sb.append(String.format("%02x", anArray));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        return null;
    }
}

From source file:Main.java

public static String md5(String str) {
    MessageDigest algorithm = null;
    try {//from   w  w w .  j  a  va 2 s . c  om
        algorithm = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (algorithm != null) {
        algorithm.reset();
        algorithm.update(str.getBytes());
        byte[] bytes = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            hexString.append(Integer.toHexString(0xFF & b));
        }
        return hexString.toString();
    }
    return "";

}

From source file:Main.java

public static byte[] HashSHA256(byte data[]) throws NoSuchAlgorithmException {
    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    return sha.digest(data);
}

From source file:Main.java

public static String getMD5(String input) {
    String result = null;//  w  ww  .j  a  v a2  s.c o m

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input.getBytes());
        byte[] digest = md.digest();

        StringBuffer sb = new StringBuffer();

        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }

        result = sb.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        return result;
    }

}

From source file:Main.java

public static String MD5Crypto(String str) {
    try {// w ww  . ja  va  2  s  .  co m
        // Create MD5 Hash
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(str.getBytes());
        byte messageDigest[] = digest.digest();
        return toHexString(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

public static byte[] md5(byte[] bytes) {
    try {/*  w w w  . j ava  2s .co m*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(bytes);
        return messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String createPubNubSafeBase64Hash(String input) {
    try {/*from w w w .java2s . co  m*/
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(input.getBytes());
        String encodedChannelName = Base64.encodeToString(messageDigest.digest(), Base64.URL_SAFE);

        //pubnub channel names cannot be more than 92 characters
        if (encodedChannelName.length() > 92) {
            encodedChannelName = encodedChannelName.substring(0, 91);
        }
        //pubnub channel names cannot have whitespace characters
        return encodedChannelName.trim();
    } catch (Exception e) {
        Log.d("X", "Error in encoding: " + e.getMessage());
        return null;
    }
}

From source file:Main.java

public static String stringGetMD5String(String s) {
    byte[] data;//from   w  ww.jav  a2  s . c  om
    MessageDigest md5;

    try {
        data = s.getBytes("UTF-8");
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        return null;
    }

    byte[] hash = md5.digest(data);
    StringBuilder hashStr = new StringBuilder();

    for (byte byteValue : hash) {
        hashStr.append(String.format("%02x", byteValue));
    }

    return hashStr.toString();
}

From source file:Main.java

/**
 * md5//w  ww .  ja v  a2s.  c  om
 * 
 * @param str
 * @return
 */
public static String md5(String str) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
        byte[] byteArray = messageDigest.digest();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                sb.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
            } else {
                sb.append(Integer.toHexString(0xFF & byteArray[i]));
            }
        }

        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return str;
}