Example usage for java.security NoSuchAlgorithmException printStackTrace

List of usage examples for java.security NoSuchAlgorithmException printStackTrace

Introduction

In this page you can find the example usage for java.security NoSuchAlgorithmException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static String sha1Hash(String text) {
    String hash = null;/*from   w  w  w  .j ava 2 s. co m*/
    try {
        final MessageDigest digest = MessageDigest.getInstance("SHA-1");
        final byte[] bytes = text.getBytes("UTF-8");
        digest.update(bytes, 0, bytes.length);
        hash = convertToHex(digest.digest());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return hash;
}

From source file:Main.java

public static String getMD5String(String str) {
    StringBuffer stringbuffer;//w ww  . j ava  2 s  . c  o  m
    MessageDigest messagedigest;
    try {
        messagedigest = MessageDigest.getInstance("MD5");
        messagedigest.update(str.getBytes());
        byte abyte0[] = messagedigest.digest();
        stringbuffer = new StringBuffer();
        for (int i = 0; i < abyte0.length; i++) {
            stringbuffer.append(HEX_DIGITS[(abyte0[i] & 0xf0) >>> 4]);
            stringbuffer.append(HEX_DIGITS[abyte0[i] & 0x0f]);
        }
        return stringbuffer.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String md5(String str) {
    MessageDigest algorithm = null;
    try {//from  ww  w. j  a v  a 2s . c  o  m
        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 String getMD5Str(String password) {
    String strResult = "";
    MessageDigest md5;/*w ww.  j ava 2s . c o m*/
    try {
        md5 = MessageDigest.getInstance("MD5");
        md5.update(password.getBytes("UTF-8"));
        byte[] bzpassword_1 = md5.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bzpassword_1.length; ++i) {
            sb.append(String.format("%02x", bzpassword_1[i]));
        }
        md5.update(sb.toString().getBytes("UTF-8"));
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String MD5(final String s) {
    final String MD5 = "MD5";
    try {//from   ww  w  . j a v  a  2  s  .  c  o m
        MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        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

private static StringBuffer getMd5Buffer(String text) {
    StringBuffer buf = new StringBuffer();
    try {//from  www .j av  a  2s. 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

public static String GetMd5(final String s) {
    try {/*from  w w w. ja v  a  2  s .com*/
        final MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(s.trim().getBytes());
        final byte[] messageDigset = digest.digest();
        return Bytes2Hex(messageDigset);
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return s;
}

From source file:Main.java

public static String stringToMD5(String string) {
    byte[] hash;/* w ww  .j ava  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

/**
 * Encrypt the plain text with MD5 algorithm.
 *
 * @param plainText The plain text.//from  w w  w  .  j  ava  2s .c o m
 * @return The Encrypt content.
 */
public static String md5Encrypt(String plainText) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(plainText.getBytes(Charset.defaultCharset()));
        return new String(toHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

static public String md5(String string) {
    try {/*from   w  w  w  . j  a v a  2 s.  c o m*/
        // Create MD5 Hash
        java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(string.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 "";
}