List of usage examples for java.security MessageDigest update
public void update(byte[] input, int offset, int len)
From source file:com.uzhnu.reedmuller.api.JsonFileCache.java
private static String getSha1Hash(String input) throws Exception { String retval = null;//from www . j a va2s .c o m ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes("UTF-8")); MessageDigest hash = MessageDigest.getInstance("SHA1"); byte[] buffer = new byte[1024]; int numRead = 0; while ((numRead = inputStream.read(buffer)) != -1) { hash.update(buffer, 0, numRead); } retval = toHexString(hash.digest()); return retval; }
From source file:edu.hm.muse.controller.Logincontroller.java
public static String calculateSHA256(InputStream is) { String output;/*from w ww.ja v a2 s . c o m*/ int read; byte[] buffer = new byte[8192]; try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); while ((read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } byte[] hash = digest.digest(); BigInteger bigInt = new BigInteger(1, hash); output = bigInt.toString(16); } catch (Exception e) { e.printStackTrace(System.err); return "0"; } return output; }
From source file:Main.java
public static String smallFileSha1(File file) { InputStream inputStream = null; try {/*from w w w .j av a 2 s . c om*/ 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
public static String getFileMd5String(File file) { if (!file.isFile()) throw new IllegalArgumentException("only file can calculate MD5 value!"); MessageDigest digest; FileInputStream in = null;/*from w w w. j av a 2s . co m*/ 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:com.amazonaws.util.Md5Utils.java
/** * Computes the MD5 hash of the data in the given input stream and returns * it as an array of bytes./*from ww w.ja v a2 s. c om*/ * Note this method closes the given input stream upon completion. */ public static byte[] computeMD5Hash(InputStream is) throws IOException { BufferedInputStream bis = new BufferedInputStream(is); try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[SIXTEEN_K]; int bytesRead; while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) { messageDigest.update(buffer, 0, bytesRead); } return messageDigest.digest(); } catch (NoSuchAlgorithmException e) { // should never get here throw new IllegalStateException(e); } finally { try { bis.close(); } catch (Exception e) { LogFactory.getLog(Md5Utils.class).debug("Unable to close input stream of hash candidate: " + e); } } }
From source file:Main.java
public static synchronized boolean downloadFileMD5Check(File f, String expectedMD5) { boolean flag = false; try {/*from w w w .j av a 2s . c o m*/ MessageDigest md = MessageDigest.getInstance("MD5"); FileInputStream fis = new FileInputStream(f); byte[] b = new byte[1024]; int len = 0; while ((len = fis.read(b)) != -1) { md.update(b, 0, len); } if (md5(md).equals(expectedMD5)) { flag = true; } fis.close(); } catch (Exception e) { e.printStackTrace(); } return flag; }
From source file:org.berlin_vegan.bvapp.acra.ACRAPostSender.java
private static String md5(String s) { MessageDigest m = null; try {//from ww w. j a v a 2s .c o m m = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } m.update(s.getBytes(), 0, s.length()); return new BigInteger(1, m.digest()).toString(16); }
From source file:Main.java
public static byte[] copyFile(InputStream is, FileOutputStream os) throws IOException { MessageDigest digester = null; try {/*from w ww .j a va 2 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
public static String getFileMD5(File file) { if (!file.isFile()) { return null; }//ww w.ja va2 s. co m MessageDigest digest = null; FileInputStream in = null; byte buffer[] = new byte[1024]; int len; try { digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer, 0, 1024)) != -1) { digest.update(buffer, 0, len); } in.close(); } catch (Exception e) { e.printStackTrace(); return null; } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); }
From source file:cc.arduino.plugins.wifi101.flashers.java.NinaFlasher.java
public static byte[] getMD5Checksum(byte[] buffer) throws Exception { try {/*from w ww. ja v a 2 s. co m*/ MessageDigest complete = MessageDigest.getInstance("MD5"); int numRead = buffer.length; if (numRead > 0) { complete.update(buffer, 0, numRead); } return complete.digest(); } catch (Exception e) { throw new Exception("Error in MD5 checksum computation."); } }