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 String getDigestValue(String s, String digestType) {
    String result = "";
    byte strTemp[] = s.getBytes();
    MessageDigest digestTemp;/*from w w w  .j  ava  2  s.co  m*/

    try {
        digestTemp = MessageDigest.getInstance(digestType);
        digestTemp.update(strTemp);

        byte[] digestValue = digestTemp.digest();

        result = bytesToHexString(digestValue);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}

From source file:Main.java

public static byte[] getFileSHA1(String path) throws IOException {

    File file = new File(path);
    FileInputStream in = new FileInputStream(file);
    MessageDigest messagedigest;/*from   ww  w .j av a2 s .  c  o  m*/
    try {
        messagedigest = MessageDigest.getInstance("SHA-1");

        byte[] buffer = new byte[1024 * 64];
        int len;

        while ((len = in.read(buffer)) > 0) {
            messagedigest.update(buffer, 0, len);
        }

        return messagedigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        throw e;
    } finally {
        in.close();
    }
    return null;
}

From source file:Main.java

public static String getBytesDigestEncrypt(byte[] bytes, String algorithm) {
    if (algorithm.equals(MD5) || algorithm.equals(SHA1) || algorithm.equals(SHA256) || algorithm.equals(SHA384)
            || algorithm.equals(SHA512)) {
        MessageDigest digest = null;
        try {//from   w w w. ja  v a  2s  . c  o  m
            digest = MessageDigest.getInstance(algorithm);
            digest.update(bytes);
            return bytes2String(digest.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.skilrock.lms.common.utility.MD5Encoder.java

public static String encode(String value) {

    try {/*from   w  ww  . j  a  v  a 2 s .c  o m*/

        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] hashMD5 = md5.digest(value.getBytes());
        return (new BASE64Encoder()).encode(hashMD5);

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * @param s String to MD5 hash//from  ww w  .  j av  a  2s . c  om
 * @return MD5 hashed string
 */
public static String md5(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

/**
 * @param s String to SHA-256 hash/* w  ww. ja v a 2 s  .  co  m*/
 * @return SHA-256 hashed string
 */
public static String sha256(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create SHA-256 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-256");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash = null;
    try {//from  w w w . ja  va2 s. co  m
        hash = MessageDigest.getInstance("MD5").digest( //No i18n
                string.getBytes("UTF-8"));//No i18n
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

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

From source file:HashUtil.java

public static String getMD5(final String data) {
    try {/*www  .ja  va  2 s. c  o m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(data.getBytes());
        BigInteger bigInt = new BigInteger(1, m.digest());
        String hashtext = bigInt.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return e.getMessage();
    }
}

From source file:Main.java

public static String hashStream(InputStream in, String hash) throws IOException {
    MessageDigest md5 = null;/*from  w ww .  j  a v  a  2  s.  com*/
    try {
        md5 = MessageDigest.getInstance(hash);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    int bsize = 4096;
    byte[] buffer = new byte[bsize];
    int length;

    while ((length = in.read(buffer, 0, bsize)) > 0) {
        md5.update(buffer, 0, length);
    }

    return toHex(md5.digest());
}

From source file:Main.java

/**
 * @param s String to SHA-1 hash/*from w  w  w.  j a v a 2  s.c o  m*/
 * @return SHA-1 hashed string
 */
public static String sha1(String s) {
    if (s == null) {
        return "";
    }
    try {
        // Create SHA-1 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
        digest.update(s.getBytes());
        byte[] messageDigest = digest.digest();
        return bytesToHex(messageDigest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}