/** * Returns a checksum of this file (also referred to as <i>hash</i> or <i>digest</i>) calculated by reading this * file's contents and feeding the bytes to the given <code>MessageDigest</code>, until EOF is reached. * * <p>The checksum is returned as an hexadecimal string, such as "6d75636f0a". The length of this string depends on * the kind of algorithm.</p> * * <p>Note: this method does not reset the <code>MessageDigest</code> after the checksum has been calculated.</p> * * @param algorithm the algorithm to use for calculating the checksum * @return this file's checksum, as an hexadecimal string * @throws IOException if an I/O error occurred while calculating the checksum * @throws NoSuchAlgorithmException if the specified algorithm does not correspond to any MessageDigest registered * with the Java Cryptography Extension. * @throws UnsupportedFileOperationException if this method relies on a file operation that is not supported * or not implemented by the underlying filesystem. */ public final String calculateChecksum(String algorithm) throws IOException, NoSuchAlgorithmException, UnsupportedFileOperationException { return calculateChecksum(MessageDigest.getInstance(algorithm)); }
/** * Returns a checksum of this file (also referred to as <i>hash</i> or <i>digest</i>) calculated by reading this * file's contents and feeding the bytes to the given <code>MessageDigest</code>, until EOF is reached. * * <p>The checksum is returned as an hexadecimal string, such as "6d75636f0a". The length of this string depends on * the kind of <code>MessageDigest</code>.</p> * * <p>Note: this method does not reset the <code>MessageDigest</code> after the checksum has been calculated.</p> * * @param messageDigest the MessageDigest to use for calculating the checksum * @return this file's checksum, as an hexadecimal string * @throws IOException if an I/O error occurred while calculating the checksum * @throws UnsupportedFileOperationException if this method relies on a file operation that is not supported * or not implemented by the underlying filesystem. */ public final String calculateChecksum(MessageDigest messageDigest) throws IOException, UnsupportedFileOperationException { InputStream in = getInputStream(); try { return calculateChecksum(in, messageDigest); } finally { in.close(); } }
/** * Calculates and returns the md5 checksum of the given <code>InputStream</code>'s contents. * The provided stream is read completely (until EOF) but is not closed. * * @param in the InputStream to digest * @return the md5 checksum of the given InputStream's contents * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ protected String calculateMd5(InputStream in) throws IOException, NoSuchAlgorithmException { return AbstractFile.calculateChecksum(in, MessageDigest.getInstance("md5")); }
private String calculateChecksum(AbstractFile file) throws IOException, NoSuchAlgorithmException { currentFileByteCounter.reset(); InputStream in = setCurrentInputStream(file.getInputStream()); try { return AbstractFile.calculateChecksum(in, MessageDigest.getInstance(CHECKSUM_VERIFICATION_ALGORITHM)); } finally { closeCurrentInputStream(); } }
assert "8350e5a3e24c153df2275c9f80692773".equals(tempFile.calculateChecksum("MD2")); assert "d41d8cd98f00b204e9800998ecf8427e".equals(tempFile.calculateChecksum("MD5")); assert "da39a3ee5e6b4b0d3255bfef95601890afd80709".equals(tempFile.calculateChecksum("SHA-1")); assert "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".equals(tempFile.calculateChecksum("SHA-256")); assert "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b".equals(tempFile.calculateChecksum("SHA-384")); assert "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e".equals(tempFile.calculateChecksum("SHA-512")); assert "00000000".equals(tempFile.calculateChecksum("CRC32")); assert "00000001".equals(tempFile.calculateChecksum("Adler32")); assert "03d85a0d629d2c442e987525319fc471".equals(tempFile.calculateChecksum("MD2")); assert "9e107d9d372bb6826bd81d3542a419d6".equals(tempFile.calculateChecksum("MD5")); assert "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12".equals(tempFile.calculateChecksum("SHA-1")); assert "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592".equals(tempFile.calculateChecksum("SHA-256")); assert "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1".equals(tempFile.calculateChecksum("SHA-384")); assert "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6".equals(tempFile.calculateChecksum("SHA-512")); assert "414fa339".equals(tempFile.calculateChecksum("CRC32")); assert "5bdc0fda".equals(tempFile.calculateChecksum("Adler32"));
String crcLine = crcReader.readLine(); crcLine = crcLine.substring(crcLine.lastIndexOf(' ')+1).trim(); String crcDest = destFile.calculateChecksum("CRC32"); if (!crcLine.equals(crcDest)) { showErrorDialog(errorDialogTitle,
if (recalculateCRC ) { origFileStream = sourceFile.getInputStream(); sourceChecksum = AbstractFile.calculateChecksum(origFileStream, MessageDigest.getInstance("CRC32")); origFileStream.close(); } else {
checksum = AbstractFile.calculateChecksum(in, digest); if(useSfvFormat) {