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:org.mozilla.android.sync.Cryptographer.java

public static KeyBundle generateKeys() {
    KeyGenerator keygen;// www. ja  va  2 s  .  c  om
    try {
        keygen = KeyGenerator.getInstance(KEY_ALGORITHM_SPEC);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

    keygen.init(KEY_SIZE);
    byte[] encryptionKey = keygen.generateKey().getEncoded();
    byte[] hmacKey = keygen.generateKey().getEncoded();
    return new KeyBundle(encryptionKey, hmacKey);
}

From source file:com.appbackr.android.tracker.Tracker.java

/**
  * This function performs md5 hashing. The reason for this function is because
  * android package does not have any default md5 hashing function.
  * @param s string encode as md5 hash//from w w w  . ja  v  a  2s  .com
  * @return md5 hash string
  */
 private static String md5(String s) {
     try {
         // Create MD5 Hash
         MessageDigest digest = MessageDigest.getInstance("MD5");
         digest.update(s.getBytes(), 0, s.length());
         return new BigInteger(1, digest.digest()).toString(16);

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

     return "";
 }

From source file:bzride.com.bzride.Utils.java

public static final String md5encrypt(final String s) {
    final String MD5 = "MD5";
    try {/* www .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
        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            String h = Integer.toHexString(0xFF & aMessageDigest);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

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

From source file:gov.nasa.jpl.analytics.util.CommonUtil.java

public static String hashString(String str) {
    String hash = "";
    try {//from www . j  a  v a  2 s .c  o m
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(str.getBytes("UTF-8"));
        byte[] digest = md.digest();
        hash = String.format("%064x", new java.math.BigInteger(1, digest)).toUpperCase();
    } catch (NoSuchAlgorithmException e) {
        LOG.error("Not a valid Hash Algorithm for String " + str);
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        LOG.error("Not a valid Encoding for String " + str);
        e.printStackTrace();
    }
    return hash;
}

From source file:eu.planets_project.tb.utils.ExperimentUtils.java

/**
 * Computes the MD5 hash of an input stream.
 * @param in The input stream to hash./*www .j  a va 2  s .co  m*/
 * @return The MD% hash, encoded as a hex string.
 */
public static String computeFixity(InputStream in) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance(FIXITY_ALG);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
    // Go through the input stream and digest.
    byte buf[] = new byte[8192];
    int n;
    try {
        while ((n = in.read(buf)) > 0) {
            md.update(buf, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
    byte hash[] = md.digest();
    return new String(Hex.encodeHex(hash));

}

From source file:com.lambdasoup.panda.PandaHttp.java

private static String generateSignature(String method, String url, String host, String secretKey,
        Map<String, String> params) {
    String queryString = canonicalQueryString(params);
    String stringToSign = method.toUpperCase() + "\n" + host + "\n" + url + "\n" + queryString;

    String signature = null;//from  w ww.  j  a  v a 2  s  .  c o m

    try {

        SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);

        byte[] rawHmac = mac.doFinal(stringToSign.getBytes());

        signature = new String(Base64.encodeBase64(rawHmac));

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    return signature;
}

From source file:com.dtolabs.rundeck.core.execution.commands.ScriptURLCommandInterpreter.java

private static String hashURL(final String url) {
    try {/*  w w w. ja  v a 2  s .com*/
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(url.getBytes(Charset.forName("UTF-8")));
        return new String(Hex.encodeHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return Integer.toString(url.hashCode());
}

From source file:io.github.goadinggoat.BungeeQuarantine.Commands.AcceptRules.java

private static String hash(String password, byte[] salt) {

    SecretKeyFactory f;/*from   ww w .j a  v  a 2s.c om*/
    SecretKey key;
    try {
        f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");

        key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen));
        return Base64.encodeBase64String(key.getEncoded());
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:ua.naiksoftware.chars.Sender.java

public static final String md5(final String s) {
    Log.d(tag, "md5 begin");
    try {/*from w ww  .ja v  a  2  s  . co  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(tag, "md5 end with hash=" + hexString);
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        Log.e(tag, "md5 algorithm excepton", e);
        e.printStackTrace();
    }
    Log.d(tag, "md5 end with empty hash");
    return "";
}

From source file:org.gatherdata.commons.security.DigestFactory.java

/**
 * Factory method to calculate a digest using the default algorithm.
 * /*from  w w  w  .  j a v a2s. co  m*/
 * @param data data to be digested
 * @return
 */
public static byte[] createDigestFor(Serializable data) {
    byte[] digest = null;
    try {
        digest = createDigestFor(data, defaultAlgorithm);
    } catch (NoSuchAlgorithmException e) {
        System.err.println("Default algorithm \"" + defaultAlgorithm + "\" not recognized by MessageDigest");
        e.printStackTrace();
    }
    return digest;
}