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.akop.bach.parser.Parser.java

protected static String sha1(String message) {
    MessageDigest md;/*from  w  ww.  j a  v  a2  s  .c o m*/

    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        if (App.getConfig().logToConsole())
            e.printStackTrace();

        return null;
    }

    try {
        md.update(message.getBytes("UTF-8"), 0, message.length());
    } catch (UnsupportedEncodingException e) {
        if (App.getConfig().logToConsole())
            e.printStackTrace();

        return null;
    }

    return String.format("%x", new BigInteger(md.digest()));
}

From source file:com.c4mprod.utils.ImageManager.java

public static String md5(String s) {
    try {//from   w  ww.j  a v 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++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        return hexString.toString();

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

From source file:com.jungle.base.utils.MiscUtils.java

public static String getHash(byte[] value) {
    try {//from w w w  .  j  a  v a  2s.c om
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(value);
        BigInteger bi = new BigInteger(md5.digest()).abs();
        return bi.toString(Character.MAX_RADIX);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return serializationBytesToHex(value);
}

From source file:com.jungle.base.utils.MiscUtils.java

public static byte[] generateMD5Binary(byte[] value) {
    if (value == null) {
        return null;
    }/*from   w ww  .  j a va2s . co m*/

    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        return md5.digest(value);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.apptentive.android.sdk.util.Util.java

private static String md5(String s) {
    try {/*from  w  w  w .j  a v a2s. c  o  m*/
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            hexString.append(Integer.toHexString(0xFF & aMessageDigest));
        }
        return hexString.toString();

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

From source file:org.jimcat.services.imagemanager.ImageUtil.java

/**
 * used to calculate the checksum of a chunk of bytes.
 * /*from   ww w.  j av a  2 s  .c  o  m*/
 * this implementation is creating an MD5 checksum.
 * 
 * @param file
 * @return - the MD5 checksum as string or null on error
 */
public static String getChecksum(byte[] file) {
    String result = null;
    try {
        // calculate checksum
        MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5");
        mdAlgorithm.update(file);
        byte[] checksum = mdAlgorithm.digest();

        // format to hex string
        StringBuffer buffer = new StringBuffer(checksum.length * 2);
        for (byte b : checksum) {
            buffer.append(Integer.toHexString((b & 0xF0) >> 4));
            buffer.append(Integer.toHexString(b & 0x0F));
        }
        result = buffer.toString().toUpperCase();

    } catch (NoSuchAlgorithmException nsae) {
        // should never happen
        nsae.printStackTrace();
    }
    return result;
}

From source file:org.owasp.webgoat.lessons.Encoding.java

/**
 * Returns the SHA hash of a String./*from  ww w . j  av a  2  s .c o m*/
 * 
 * @param str
 *            Description of the Parameter
 * @return Description of the Return Value
 */

public static String hashSHA(String str) {
    byte[] b = str.getBytes();
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
        md.update(b);
    } catch (NoSuchAlgorithmException e) {
        // it's got to be there
        e.printStackTrace();
    }
    return (base64Encode(md.digest()));
}

From source file:org.owasp.webgoat.lessons.Encoding.java

/**
 * Returns the MD5 hash of a String./*from  ww w .j  a v a  2  s.c om*/
 * 
 * @param str
 *            Description of the Parameter
 * @return Description of the Return Value
 */

public static String hashMD5(String str) {

    byte[] b = str.getBytes();
    MessageDigest md = null;

    try {
        md = MessageDigest.getInstance("MD5");
        md.update(b);
    } catch (NoSuchAlgorithmException e) {
        // it's got to be there
        e.printStackTrace();
    }
    return (base64Encode(md.digest()));
}

From source file:org.csp.everyaware.db.Record.java

public static String md5(String s) {
    try {/*  w w w. j  a  va2  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++)
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        */

        return convertToHex(messageDigest);

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

From source file:org.openintents.safe.CryptoHelper.java

/**
 * Generate a random salt for use with the cipher.
 *
 * @return String version of the 8 byte salt
 * @author Randy McEoin/*from ww  w .ja  v a 2s.  c  o m*/
 */
public static String generateSalt() throws NoSuchAlgorithmException {
    byte[] salt = new byte[8];
    SecureRandom sr;
    try {
        sr = SecureRandom.getInstance("SHA1PRNG");
        sr.nextBytes(salt);
        if (debug) {
            Log.d(TAG, "generateSalt: salt=" + salt.toString());
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        throw e;
    }
    return toHexString(salt);
}