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 string) {
    byte[] hash;/*  www.  j av a 2 s  . c om*/
    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 MD5(String md5) {
    if (md5 == null) {
        return null;
    }/*www .  j a v  a2s. com*/
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuilder sb = new StringBuilder();
        for (int a = 0; a < array.length; a++) {
            sb.append(Integer.toHexString((array[a] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String md5(String string) {

    byte[] hash;//w  ww. j  a v  a2s.com

    try {

        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));

    } catch (NoSuchAlgorithmException e) {

        throw new RuntimeException("Huh, MD5 should be supported?", e);

    } catch (UnsupportedEncodingException e) {

        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
    }

    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 getMD5Str(String md5Str) {
    byte[] hash;/*from w ww  .  j av  a 2 s  .  c o m*/
    try {
        hash = MessageDigest.getInstance("MD5").digest(md5Str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, MD5 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
    }

    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 md5(String string) {
    byte[] hash;//from   w ww.  ja  v  a2  s . c om

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, MD5 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);

    for (byte b : hash) {
        int i = (b & 0xFF);
        if (i < 0x10)
            hex.append('0');
        hex.append(Integer.toHexString(i));
    }

    return hex.toString();
}

From source file:Main.java

public static byte[] simpleHash256(byte[] msg) throws NoSuchAlgorithmException {
    MessageDigest sha256 = MessageDigest.getInstance(SHA_256);
    byte[] byteHolder1, byteHolder2;
    byteHolder1 = sha256.digest(msg);//from   ww w . j  av  a  2 s .c  o  m
    for (int i = 0; i < 100; i++) {
        byteHolder2 = sha256.digest(byteHolder1);
        byteHolder1 = sha256.digest(byteHolder2);
    }
    return byteHolder1;
}

From source file:Main.java

public static byte[] SHA1(String text) {
    if (null == text) {
        return null;
    }/* w  w  w  .  j  a  v  a 2 s.  c o  m*/
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        messageDigest.update(text.getBytes());
        return messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static StringBuffer getMd5Buffer(String text) {
    StringBuffer buf = new StringBuffer();
    try {//  w w  w. j  ava 2 s . com
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(text.getBytes());
        byte[] b = md.digest();
        int i;
        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));
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return buf;
}

From source file:Main.java

private static byte[] getSHA1Digest(String data) throws IOException {
    byte[] bytes = null;
    try {/*from  www  .jav a 2s.  co m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        bytes = md.digest(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
    }
    return bytes;
}

From source file:Main.java

private static byte[] getMD5Digest(String data) throws IOException {
    byte[] bytes = null;
    try {//from ww w . ja va  2s .  c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        bytes = md.digest(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
        throw new IOException(gse);
    }
    return bytes;
}