Java Reference
In-Depth Information
Example 6−4: Manifest.java (continued)
System.exit(0);
}
// Tell the user we're done with this lengthy computation
System.out.println("verified.");
}
// Tell the user we're starting the next phase of verification
System.out.print("Verifying file message digests");
System.out.flush();
// Get a MessageDigest object to compute digests
MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
// Loop through all files
for(int i = 0; i < numfiles; i++) {
String filename = (String)files.get(i);
// Look up the encoded digest from the manifest file
String hexdigest = manifest.getProperty(filename);
// Compute the digest for the file.
byte[] digest;
try { digest = getFileDigest(filename, md); }
catch (IOException e) {
System.out.println("\nSkipping " + filename + ": " + e);
continue;
}
// Encode the computed digest and compare it to the encoded digest
// from the manifest. If they are not equal, print an error
// message.
if (!hexdigest.equals(hexEncode(digest)))
System.out.println("\nFile '" + filename +
"' failed verification.");
// Send one dot of output for each file we process. Since
// computing message digests takes some time, this lets the user
// know that the program is functioning and making progress
System.out.print(".");
System.out.flush();
}
// And tell the user we're done with verification.
System.out.println("done.");
}
/**
* This convenience method is used by both create() and verify(). It
* reads the contents of a named file and computes a message digest
* for it, using the specified MessageDigest object.
**/
public static byte[] getFileDigest(String filename, MessageDigest md)
throws IOException {
// Make sure there is nothing left behind in the MessageDigest
md.reset();
// Create a stream to read from the file and compute the digest
DigestInputStream in =
new DigestInputStream(new FileInputStream(filename),md);
// Read to the end of the file, discarding everything we read.
Search WWH ::




Custom Search