Reputation: 71
I have a application that is ckecking if an audiofile is edited. After that, I should come to know if someone has edited this audio file. I am thinking of adding a water mark to the audio file to later check if the watermark has changed or not and know if audio file was edited.
Also, is there another way for audio edit protection in java?
Upvotes: 1
Views: 474
Reputation: 635
There is a method that applies to any file, not just audio. You can compute and compare checksums using hashing algorithm.
Java has this feature built in in java.security.MessageDigest
package.
To get the checksum, consider this code snippet:
try (FileInputStream inputStream = new FileInputStream(file)) {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bytesBuffer = new byte[1024];
int bytesRead = -1;
while ((bytesRead = inputStream.read(bytesBuffer)) != -1) {
digest.update(bytesBuffer, 0, bytesRead);
}
byte[] hashedBytes = digest.digest();
return convertByteArrayToHexString(hashedBytes);
} catch (NoSuchAlgorithmException | IOException ex) {
throw new HashGenerationException(
"Could not generate hash from file", ex);
}
This method body returns the string representation of Hashed Array of bytes for your input file. You can call this code on the original and suspect files and do the comparison. If strings match, files are identical.
Code snipped uses fast MD5 angorithm, but you can choose something more secure from here:
Upvotes: 2