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 sha1(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    md.update(input.getBytes("UTF-8"));
    byte[] output = md.digest();
    return bytesToHex(output);
}

From source file:Main.java

public static String stringToMD5(String string) {
    byte[] hash;/*from   ww  w .  j  av  a2s .  c o m*/

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }

    return hex.toString();
}

From source file:Main.java

public static String SHA256(String text) throws NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance("SHA-256");

    md.update(text.getBytes());//from ww w.  ja v  a 2s .  co  m
    byte[] digest = md.digest();

    return Base64.encodeToString(digest, Base64.DEFAULT);
}

From source file:Main.java

static MessageDigest getDigest(String algorithm) {
    try {//from   www  .j a va  2 s  .  com
        return MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

public static String stringToMD5(String string) {
    byte[] hash;//from   w w  w. jav a2s.c o m

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }

    return hex.toString().substring(8, 24);
}

From source file:Main.java

public static String md5(String str) {
    String cacheKey;/* w  w  w  .ja  va 2 s.co  m*/
    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 final String md5(String tps) {
    final String MD5 = "MD5";
    try {//w  ww .j  av  a  2s.c  o m
        // Create MD5 Hash
        MessageDigest digest = MessageDigest.getInstance(MD5);
        digest.update(tps.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 md5(final String s) {
    final String MD5 = "MD5";
    try {//from ww  w  . ja va  2  s.c om
        // Create MD5 Hash
        MessageDigest digest = MessageDigest.getInstance(MD5);
        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 md5s(String plainText) {
    String str = "";
    try {// w  w w  .j a  va 2s  . c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        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));
        }
        str = buf.toString();

    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

public static String MD5(String text) {
    StringBuffer buffer;/*from  ww  w. jav a  2s.co m*/
    MessageDigest digest;
    byte[] data;
    int i, n;

    buffer = new StringBuffer();

    try {
        digest = MessageDigest.getInstance("MD5");
        digest.update(text.getBytes());
        data = digest.digest();

        for (i = 0, n = data.length; i < n; ++i) {
            buffer.append(Integer.toHexString(0xff & data[i]));
        }
    } catch (Exception e) {
    }

    return buffer.toString();
}