List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
private static String sha1Hash(String toHash) { String hash = null;// w w w. j a v a2 s. co m try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); byte[] bytes = toHash.getBytes("UTF-8"); digest.update(bytes, 0, bytes.length); bytes = digest.digest(); // This is ~55x faster than looping and String.formating() hash = bytesToHex(bytes); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return hash; }
From source file:Main.java
public static byte[] SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md;/*from ww w . j a v a 2 s. c om*/ md = MessageDigest.getInstance("SHA-1"); byte[] sha1hash = new byte[40]; md.update(text.getBytes(), 0, text.length()); // "iso-8859-1" sha1hash = md.digest(); return (sha1hash); }
From source file:Main.java
/** * //from w w w . ja v a 2s. c o m * @param word * the string to be hashed * @return SHA1-hash of the word */ public static String hash(String word) { byte[] data = word.getBytes(); MessageDigest md = null; try { md = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } byte[] b = md.digest(data); String result = ""; for (int i = 0; i < b.length; i++) { result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1); } return result; }
From source file:Main.java
public static String getPwdHash(String password, String salt) { try {/*from ww w. ja va 2 s . c o m*/ String temp = password + salt; MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(temp.getBytes("UTF-8")); // Change this to "UTF-16" if needed byte[] digest = md.digest(); return String.format("%0" + (digest.length * 2) + 'x', new BigInteger(1, digest)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * A hashing method that changes a string (like a URL) into a hash suitable * for using as a disk filename.//from ww w .jav a2 s.co m */ public static String hashKeyForUrl(String key) { String cacheKey; try { final MessageDigest mDigest = MessageDigest.getInstance("MD5"); mDigest.update(key.getBytes()); cacheKey = bytesToHexString(mDigest.digest()); } catch (NoSuchAlgorithmException e) { cacheKey = String.valueOf(key.hashCode()); } return cacheKey; }
From source file:Main.java
/** * Compute the MD5 hash of the byte array provided. Does not accumulate * input./*ww w . j a va2 s. co m*/ * @param in the byte array to hash * @return the MD5 hash of the byte array */ public static byte[] computeMd5Hash(byte[] in) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); return md5.digest(in); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:Main.java
private static String getCertificateSHA1(X509Certificate certificate) throws NoSuchAlgorithmException, CertificateEncodingException { MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); byte[] der = certificate.getEncoded(); messageDigest.update(der);/*w w w . j a v a 2 s . co m*/ byte[] digest = messageDigest.digest(); return hexify(digest); }
From source file:Main.java
private static String encode(String str, String method) { MessageDigest md = null;/*w w w .ja va2s . co m*/ String dstr = null; try { md = MessageDigest.getInstance(method); md.update(str.getBytes()); dstr = new BigInteger(1, md.digest()).toString(16); int dstrCount = 32 - dstr.length(); for (int i = 0; i < dstrCount; i++) { dstr = "0" + dstr; } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return dstr; }
From source file:Main.java
private static byte[] hashTemplate(byte[] data, String algorithm) { if (data == null || data.length <= 0) return null; try {/*from w w w. j av a 2 s . c o m*/ MessageDigest md = MessageDigest.getInstance(algorithm); md.update(data); return md.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static String md5Calc(File f) { int i;//from ww w. j av a2 s .c om byte[] buffer = new byte[1024]; int read = 0; String md5hash; try { MessageDigest digest = MessageDigest.getInstance("MD5"); InputStream is = new FileInputStream(f); while ((read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); md5hash = bigInt.toString(16); is.close(); } catch (Exception e) { e.printStackTrace(); return null; } if (md5hash.length() != 33) { String tmp = ""; for (i = 1; i < (33 - md5hash.length()); i++) { tmp = tmp.concat("0"); } md5hash = tmp.concat(md5hash); } return md5hash; }