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

private static byte[] generateKey(String strSrc) {

    MessageDigest digest;/*  ww  w  .j a  v a  2 s .  c om*/

    byte[] keyBytes = new byte[32];
    try {
        digest = MessageDigest.getInstance("SHA-256");
        digest.update(strSrc.getBytes("UTF-8"));
        System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return keyBytes;
}

From source file:Main.java

private static String md5(String in) {
    MessageDigest digest;/*from w  ww  . j  a v  a 2 s .c o m*/
    try {
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        byte[] a = digest.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String hashString(String input) {
    MessageDigest md5;//ww w . j a v a2s. co  m
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return String.valueOf(input.hashCode());
    }
    md5.update(input.getBytes());
    byte[] hash = md5.digest();
    return toHex(hash);
}

From source file:Main.java

public static String encode(String string) {
    try {//  w ww  . j a va 2s  . c o m
        MessageDigest e = MessageDigest.getInstance("MD5");
        return bytesToHexString(e.digest(string.getBytes()));
    } catch (NoSuchAlgorithmException var2) {
        var2.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String MD5(String string) {
    byte[] hash;// w  w w.j  a v  a2 s  .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 SHA1(String s) {
    try {/*from  ww w .jav a  2s  .  c o  m*/
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();
        return toHexString(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String getHash(String input) {
    if (input == null || input.equals("") || input.isEmpty()) {
        return "";
    }/*from  w w w  . j ava  2 s  .  c  o  m*/

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] messageDigest = md.digest(input.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            sb.append(Integer.toString((messageDigest[i] & 0xff) + 0x100, 16).substring(1));
        }

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

From source file:Main.java

public static String getMd5String(String md5) {
    try {/*from  ww w .j  a  va  2 s  . c  o m*/
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        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 (java.security.NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String encode(String str, String method) {
    MessageDigest md = null;// www. ja  v  a  2  s . c o m
    String dstr = null;
    try {
        md = MessageDigest.getInstance(method);
        md.update(str.getBytes());
        dstr = new BigInteger(1, md.digest()).toString(16);
        int dstrCount = 32 - dstr.length();
        for (int i = 0; i < dstrCount; i++) {
            dstr = "0" + dstr;
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return dstr;
}

From source file:Main.java

public static String encode(String text) {
    try {/* w  w  w  .  j  a  v  a  2  s .  co m*/
        MessageDigest digest = MessageDigest.getInstance("md5");
        byte[] result = digest.digest(text.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte b : result) {
            int number = b & 0xff;
            String hex = Integer.toHexString(number);
            if (hex.length() == 1) {
                sb.append("0" + hex);
            } else {
                sb.append(hex);
            }
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}