Java Reference
In-Depth Information
Example 6−4: Manifest.java (continued)
}
// If -v was specified or no file were given, verify a manifest
// Otherwise, create a new manifest for the specified files
if (verify || (filelist.size() == 0)) verify(manifestfile, keystore);
else create(manifestfile, digestAlgorithm,
signername, signatureAlgorithm,
keystore, password, filelist);
}
/**
* This method creates a manifest file with the specified name, for
* the specified vector of files, using the named message digest
* algorithm. If signername is non-null, it adds a digital signature
* to the manifest, using the named signature algorithm. This method can
* throw a bunch of exceptions.
**/
public static void create(String manifestfile, String digestAlgorithm,
String signername, String signatureAlgorithm,
KeyStore keystore, String password,
List filelist)
throws NoSuchAlgorithmException, InvalidKeyException,
SignatureException, KeyStoreException,
UnrecoverableKeyException, IOException
{
// For computing a signature, we have to process the files in a fixed,
// repeatable order, so sort them alphabetically.
Collections.sort(filelist);
int numfiles = filelist.size();
Properties manifest = new Properties(), metadata = new Properties();
MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
Signature signature = null;
byte[] digest;
// If a signer name was specified, then prepare to sign the manifest
if (signername != null) {
// Get a Signature object
signature = Signature.getInstance(signatureAlgorithm);
// Look up the private key of the signer from the keystore
PrivateKey key = (PrivateKey)
keystore.getKey(signername, password.toCharArray());
// No prepare to create a signature for the specified signer
signature.initSign(key);
}
// Now, loop through the files, in a well-known alphabetical order
System.out.print("Computing message digests");
for(int i = 0; i < numfiles; i++) {
String filename = (String)filelist.get(i);
// Compute the digest for each, and skip files that don't exist.
try { digest = getFileDigest(filename, md); }
catch (IOException e) {
System.err.println("\nSkipping " + filename + ": " + e);
continue;
}
Search WWH ::




Custom Search