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:com.dianping.wed.cache.redis.util.TestDataUtil.java

/**
 * MD5/*  ww w.  j  a  va 2  s.co m*/
 *
 * @param str ?
 * @return ?
 */
public static String toMD5(String str) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] byteDigest = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < byteDigest.length; offset++) {
            i = byteDigest[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        //32?
        return buf.toString();
        // 16?
        //return buf.toString().substring(8, 24);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.cloudbase.cbhelperdemo.SettingsScreen.java

private static String md5(String s) {
    try {/* w w w .j av a2 s  . c om*/
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++)
            hexString.append(String.format("%02x", messageDigest[i]));
        return hexString.toString();

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

From source file:com.aqnote.shared.cryptology.digest.MD.java

public final static String md2(byte[] src) {
    if (src == null)
        return "";
    try {/*from   w w w  .  j a  v a 2 s . c  om*/
        // MessageDigest messageDigest = MessageDigest.getInstance("MD2");
        MessageDigest messageDigest = MessageDigest.getInstance(OID_MD2, JCE_PROVIDER);
        messageDigest.update(src);
        return new String(ByteUtil.toHexBytes(messageDigest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.aqnote.shared.cryptology.digest.MD.java

public final static String md4(byte[] src) {
    if (src == null)
        return "";
    try {//  w  w w  .ja v a 2s. c  om
        // MessageDigest messageDigest = MessageDigest.getInstance("MD4");
        MessageDigest messageDigest = MessageDigest.getInstance(OID_MD4, JCE_PROVIDER);
        messageDigest.update(src);
        return new String(ByteUtil.toHexBytes(messageDigest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String md5(InputStream in) {
    int bufferSize = 256 * 1024;
    DigestInputStream digestInputStream = null;
    try {/* www  .  ja v a  2  s. c o  m*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        digestInputStream = new DigestInputStream(in, messageDigest);
        byte[] buffer = new byte[bufferSize];
        while (digestInputStream.read(buffer) > 0)
            ;
        messageDigest = digestInputStream.getMessageDigest();
        byte[] resultByteArray = messageDigest.digest();
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] resultCharArray = new char[resultByteArray.length * 2];
        int index = 0;
        for (byte b : resultByteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        return new String(resultCharArray);
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (digestInputStream != null)
                digestInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static String encode(InputStream in) {
    int bufferSize = 256 * 1024;
    DigestInputStream digestInputStream = null;
    try {//from   w w w.ja  v a2 s . c o m
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        digestInputStream = new DigestInputStream(in, messageDigest);
        byte[] buffer = new byte[bufferSize];
        while (digestInputStream.read(buffer) > 0)
            ;
        messageDigest = digestInputStream.getMessageDigest();
        byte[] resultByteArray = messageDigest.digest();
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] resultCharArray = new char[resultByteArray.length * 2];
        int index = 0;
        for (byte b : resultByteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        return new String(resultCharArray);
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (digestInputStream != null)
                digestInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.aqnote.shared.cryptology.digest.MD.java

public final static String md5(byte[] src) {
    if (src == null)
        return "";
    try {// w  w  w.j av a  2 s. c  o m
        // MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        MessageDigest messageDigest = MessageDigest.getInstance(OID_MD5, JCE_PROVIDER);

        messageDigest.update(src);
        return new String(ByteUtil.toHexBytes(messageDigest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static byte[] getSHA1hash(byte[] dataByte) {
    MessageDigest md = null;/*from w  ww.ja  va2 s  .  com*/
    byte[] sha1hash = new byte[20];
    try {
        md = MessageDigest.getInstance("SHA-1");
        md.update(dataByte, 0, dataByte.length);
        sha1hash = md.digest();
        return sha1hash;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return null;
}

From source file:com.jredrain.base.utils.Digests.java

public static String sha1(String decript) {
    try {/*  w  ww .  jav  a2  s  .  c o m*/
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(decript.getBytes());
        byte messageDigest[] = digest.digest();
        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        // ? ?? 
        for (int i = 0; i < messageDigest.length; i++) {
            String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();

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

From source file:org.springframework.integration.aws.common.AWSTestUtils.java

/**
 * Helper method that will be used to generate the base64 encoded MD5 hash of the string
 *
 * @param input//ww w  .  j a v a2s . c o m
 */
public static String md5Hash(String input) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] digestedBytes = digest.digest(input.getBytes("UTF-8"));
        return new String(Base64.encodeBase64(digestedBytes), "UTF-8");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}