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 MD5(String md5) {
    if (md5 == null) {
        return null;
    }/*from ww w.  j  av  a 2 s.c o  m*/
    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 SHA256Encrypt(String orignal) {
    MessageDigest md = null;/*from   ww  w  . j  a  v a  2 s. c  o  m*/
    try {
        md = MessageDigest.getInstance(ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (null != md) {
        byte[] origBytes = orignal.getBytes();
        md.update(origBytes);
        byte[] digestRes = md.digest();
        String digestStr = getDigestStr(digestRes);
        return digestStr;
    }

    return null;
}

From source file:Main.java

public static String signature(String source) {

    try {/*from   w w  w  .j a v  a 2  s . co  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.reset();
        md.update(source.getBytes());

        byte[] mdbytes = md.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
            String hex = Integer.toHexString(0xff & mdbytes[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();

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

From source file:Main.java

public static String hashMD5Generate(String s) {
    try {/*w  ww  .  j av a  2 s .  c  o  m*/
        // 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++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        Log.d(DEBUG, hexString.toString());
        return hexString.toString();

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

From source file:Main.java

public static String encryption(String plainText) {
    String re_md5 = "";
    try {//  www .j av a2  s  .co  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();
        int i = 0;

        StringBuilder sb = new StringBuilder("");
        for (int offset = 0, len = b.length; offset < len; offset++) {
            i = b[offset];
            if (i < 0) {
                i += 256;
            }
            if (i < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(i));
        }
        re_md5 = sb.toString();

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

From source file:Main.java

public static String md5(String input) {
    String result = input;//from  ww w.j  a v a2  s.  co m
    if (input != null) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            if ((result.length() % 2) != 0) {
                result = "0" + result;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static String md5L(String input) {
    try {//from ww  w.j av  a2  s  .  c o  m
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(input.getBytes());
        byte[] md = mdInst.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte aMd : md) {
            String shaHex = Integer.toHexString(aMd & 0xFF);
            if (shaHex.length() < 2) {
                hexString.append(0);
            }
            hexString.append(shaHex);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String md5(final String c) {
    if (md == null) {
        try {//from w w  w . j  ava2s.co m
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    if (md != null) {
        md.update(c.getBytes());
        return byte2hex(md.digest());
    }
    return "";
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash = null;
    try {// www. j a  v a  2  s. c o  m
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } 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");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

public static String getHash(String stringToHash) {

    MessageDigest digest = null;// w w w .  j  a v a 2  s. c o  m
    try {
        digest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    byte[] result = null;

    try {
        result = digest.digest(stringToHash.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    StringBuilder sb = new StringBuilder();

    for (byte b : result) {
        sb.append(String.format("%02X", b));
    }

    String messageDigest = sb.toString();
    return messageDigest;
}