List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
public static byte[] copyFile(InputStream is, FileOutputStream os) throws IOException { MessageDigest digester = null; try {//from ww w. ja v a2 s. c o m digester = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[16 * 1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); digester.update(buffer, 0, length); } os.flush(); os.close(); //os.getFD().sync(); //Log.d(TAG, "done copying"); } catch (Exception e) { e.printStackTrace(); } finally { is.close(); Log.d(TAG, "done copying"); } if (digester != null) { byte[] digest = digester.digest(); return digest; } return null; }
From source file:Main.java
/** * Calculates an MD5 hash for a text.//from w w w.jav a 2 s .c o m * * @param dataToEncode * The data to encode. * @return A text representation of a hexadecimal value of length 32. */ public static String messageDigestFive(final String dataToEncode) { MessageDigest m; try { m = MessageDigest.getInstance("MD5"); byte[] data = dataToEncode.getBytes(); m.update(data, 0, data.length); BigInteger i = new BigInteger(1, m.digest()); return String.format("%1$032X", i); } catch (NoSuchAlgorithmException e) { // MD5 Should be supported by the runtime! throw new IllegalStateException(e); } }
From source file:Main.java
/** * Returns the lowercase string representation of the file's MD5 sum. *//* w w w . jav a 2 s .c om*/ public static String getMD5Sum(String file) throws IOException { try { MessageDigest digest = MessageDigest.getInstance("MD5"); InputStream is = new FileInputStream(file); byte[] buffer = new byte[8192]; int read = 0; while ((read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } is.close(); byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); return bigInt.toString(16); } catch (NoSuchAlgorithmException e) { return ""; } }
From source file:Main.java
public static String MD5(String string) { String result = null;/*from w ww. ja va 2s. c om*/ try { char[] charArray = string.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) { byteArray[i] = (byte) charArray[i]; } StringBuffer hexValue = new StringBuffer(); byte[] md5Bytes = MessageDigest.getInstance("MD5").digest(byteArray); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } result = hexValue.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static String getFileMd5String(File file) { if (!file.isFile()) throw new IllegalArgumentException("only file can calculate MD5 value!"); MessageDigest digest;//from w w w .ja v a 2s. c o m FileInputStream in = null; byte buffer[] = new byte[8192]; int len; try { digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer)) != -1) { digest.update(buffer, 0, len); } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); } catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (in != null) in.close(); } catch (Exception e) { e.printStackTrace(); } } }
From source file:Main.java
private static String md5(final String s) { final String MD5 = "MD5"; try {//from ww w. j a v a2s. c o m MessageDigest digest = MessageDigest.getInstance(MD5); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); 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:Main.java
public static byte[] getFileSHA1(String path) throws IOException { File file = new File(path); FileInputStream in = new FileInputStream(file); MessageDigest messagedigest;/* w w w . ja v a2s. c o m*/ try { messagedigest = MessageDigest.getInstance("SHA-1"); byte[] buffer = new byte[1024 * 64]; int len; while ((len = in.read(buffer)) > 0) { messagedigest.update(buffer, 0, len); } return messagedigest.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (OutOfMemoryError e) { e.printStackTrace(); throw e; } finally { in.close(); } return null; }
From source file:Main.java
public static String smallFileSha1(File file) { InputStream inputStream = null; try {/*from www .j ava2s .c o m*/ MessageDigest md = MessageDigest.getInstance("SHA1"); inputStream = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024]; int nRead = 0; while ((nRead = inputStream.read(buffer)) != -1) { md.update(buffer, 0, nRead); } byte[] digest = md.digest(); byte[] blockBytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(1).array(); byte[] result = ByteBuffer.allocate(4 + digest.length).put(blockBytes, 0, 4) .put(digest, 0, digest.length).array(); return Base64.encodeToString(result, Base64.URL_SAFE | Base64.NO_WRAP); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
/** * Returns the Md5 hash for the given text. * //from ww w. j a v a 2 s . com * @param text * String whose Md5 hash should be returned. * @return Returns the Md5 hash for the given text. */ public static String getMd5Hash(String text) { StringBuffer result = new StringBuffer(32); try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(text.getBytes()); Formatter f = new Formatter(result); byte[] digest = md5.digest(); for (int i = 0; i < digest.length; i++) { f.format("%02x", new Object[] { new Byte(digest[i]) }); } } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } return result.toString(); }
From source file:Main.java
/** * This method uses MessageDigest class to convert the passed string to * SHA-1 hexadecimal text./*from w w w . j a v a 2 s.c o m*/ * * @param text Input string * @return Return SHA-1 value */ public static String convertToSHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { final MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] sha1hash = new byte[30]; md.update(text.getBytes("iso-8859-1"), 0, text.length()); sha1hash = md.digest(); return convertToHex(sha1hash); }