Created
April 10, 2015 12:28
-
-
Save khusnetdinov/645bf6e24072a4c0d197 to your computer and use it in GitHub Desktop.
Checksum compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Boolean checkClonedFilesChecksum(String source, String result) throws IOException, NoSuchAlgorithmException { | |
String sourceChecksum = getChecksum(source); | |
String resultChecksum = getChecksum(result); | |
System.out.println("Source checksum: " + sourceChecksum); | |
System.out.println("Result checksum: " + resultChecksum); | |
return sourceChecksum.equals(resultChecksum); | |
} | |
private static String getChecksum(String fileName) throws IOException, NoSuchAlgorithmException { | |
MessageDigest msg = MessageDigest.getInstance("SHA1"); | |
FileInputStream fis = new FileInputStream(fileName); | |
StringBuffer buffer = new StringBuffer(""); | |
Integer readBytes; | |
byte[] dataBytes = new byte[1024]; | |
byte[] msgBytes = msg.digest(); | |
while ((readBytes = fis.read(dataBytes)) != -1) { | |
msg.update(dataBytes, 0, readBytes); | |
} | |
for (int i = 0; i < msgBytes.length; i += 1) { | |
buffer.append(Integer.toString((msgBytes[i] & 0xff) + 0x100, 16).substring(1)); | |
} | |
fis.close(); | |
return buffer.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment