sys.util.jar.Manifest

Here are the examples of the java api sys.util.jar.Manifest taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

4 Examples 7

18 View Source File : ZipSigner.java , License : Apache License 2.0 , Project Creator : SnowVolf
/**
 * Write the signature file to the given output stream.
 */
private void generateSignatureFile(Manifest manifest, OutputStream out) throws IOException, GeneralSecurityException {
    out.write(("Signature-Version: 1.0\r\n").getBytes());
    out.write(("Created-By: 1.0 (Android SignApk)\r\n").getBytes());
    // BASE64Encoder base64 = new BASE64Encoder();
    MessageDigest md = MessageDigest.getInstance("SHA1");
    PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true, "UTF-8");
    // Digest of the entire manifest
    manifest.write(print);
    print.flush();
    out.write(("SHA1-Digest-Manifest: " + Base64.encode(md.digest()) + "\r\n\r\n").getBytes());
    Map<String, Attributes> entries = manifest.getEntries();
    for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
        if (canceled)
            break;
        progressHelper.progress(ProgressEvent.PRORITY_NORMAL, resourceAdapter.getString(ResourceAdapter.Item.GENERATING_SIGNATURE_FILE));
        // Digest of the manifest stanza for this entry.
        String nameEntry = "Name: " + entry.getKey() + "\r\n";
        print.print(nameEntry);
        for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();
        out.write(nameEntry.getBytes());
        out.write(("SHA1-Digest: " + Base64.encode(md.digest()) + "\r\n\r\n").getBytes());
    }
}
14 View Source File : ZipSigner.java , License : Apache License 2.0 , Project Creator : SnowVolf
/**
 * Copy all the files in a manifest from input to output.  We set
 * the modification times in the output to a fixed time, so as to
 * reduce variation in the output file and make incremental OTAs
 * more efficient.
 */
private void copyFiles(Manifest manifest, Map<String, ZioEntry> input, ZipOutput output, long timestamp) throws IOException {
    Map<String, Attributes> entries = manifest.getEntries();
    List<String> names = new ArrayList<String>(entries.keySet());
    Collections.sort(names);
    int i = 1;
    for (String name : names) {
        if (canceled)
            break;
        progressHelper.progress(ProgressEvent.PRORITY_NORMAL, resourceAdapter.getString(ResourceAdapter.Item.COPYING_ZIP_ENTRY, i, names.size()));
        i += 1;
        ZioEntry inEntry = input.get(name);
        inEntry.setTime(timestamp);
        output.write(inEntry);
    }
}
13 View Source File : ZipSigner.java , License : Apache License 2.0 , Project Creator : SnowVolf
/**
 * Sign the
 *  and signature block template.  The signature block template
 *  parameter may be null, but if so
 *  android-sun-jarsign-support.jar must be in the classpath.
 */
public void signZip(Map<String, ZioEntry> zioEntries, OutputStream outputStream, String outputZipFilename) throws IOException, GeneralSecurityException {
    boolean debug = getLogger().isDebugEnabled();
    progressHelper.initProgress();
    if (keySet == null) {
        if (!keymode.startsWith(MODE_AUTO))
            throw new IllegalStateException("No keys configured for signing the file!");
        // Auto-determine which keys to use
        String keyName = this.autoDetectKey(keymode, zioEntries);
        if (keyName == null)
            throw new AutoKeyException(resourceAdapter.getString(ResourceAdapter.Item.AUTO_KEY_SELECTION_ERROR, new File(outputZipFilename).getName()));
        autoKeyObservable.notifyObservers(keyName);
        loadKeys(keyName);
    }
    ZipOutput zipOutput = null;
    try {
        zipOutput = new ZipOutput(outputStream);
        if (KEY_NONE.equals(keySet.getName())) {
            progressHelper.setProgressTotalItems(zioEntries.size());
            progressHelper.setProgressCurrentItem(0);
            copyFiles(zioEntries, zipOutput);
            return;
        }
        // Calculate total steps to complete for accurate progress percentages.
        int progressTotalItems = 0;
        for (ZioEntry entry : zioEntries.values()) {
            String name = entry.getName();
            if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !name.equals(CERT_SF_NAME) && !name.equals(CERT_RSA_NAME) && (stripPattern == null || !stripPattern.matcher(name).matches())) {
                // digest for manifest, digest in sig file, copy data
                progressTotalItems += 3;
            }
        }
        // CERT.RSA generation
        progressTotalItems += 1;
        progressHelper.setProgressTotalItems(progressTotalItems);
        progressHelper.setProgressCurrentItem(0);
        // Assume the certificate is valid for at least an hour.
        long timestamp = keySet.getPublicKey().getNotBefore().getTime() + 3600L * 1000;
        // MANIFEST.MF
        // progress(ProgressEvent.PRORITY_NORMAL, JarFile.MANIFEST_NAME);
        Manifest manifest = addDigestsToManifest(zioEntries);
        if (canceled)
            return;
        ZioEntry ze = new ZioEntry(JarFile.MANIFEST_NAME);
        ze.setTime(timestamp);
        manifest.write(ze.getOutputStream());
        zipOutput.write(ze);
        // CERT.SF
        ze = new ZioEntry(CERT_SF_NAME);
        ze.setTime(timestamp);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        generateSignatureFile(manifest, out);
        if (canceled)
            return;
        byte[] sfBytes = out.toByteArray();
        if (debug) {
            getLogger().debug("Signature File: \n" + new String(sfBytes) + "\n" + HexDumpEncoder.encode(sfBytes));
        }
        ze.getOutputStream().write(sfBytes);
        zipOutput.write(ze);
        // CERT.RSA
        progressHelper.progress(ProgressEvent.PRORITY_NORMAL, resourceAdapter.getString(ResourceAdapter.Item.GENERATING_SIGNATURE_BLOCK));
        ze = new ZioEntry(CERT_RSA_NAME);
        ze.setTime(timestamp);
        writeSignatureBlock(keySet, sfBytes, ze.getOutputStream());
        zipOutput.write(ze);
        if (canceled)
            return;
        // Everything else
        copyFiles(manifest, zioEntries, zipOutput, timestamp);
        if (canceled)
            return;
    } finally {
        zipOutput.close();
        if (canceled) {
            try {
                if (outputZipFilename != null)
                    new File(outputZipFilename).delete();
            } catch (Throwable t) {
                getLogger().warning(t.getClass().getName() + ":" + t.getMessage());
            }
        }
    }
}
12 View Source File : ZipSigner.java , License : Apache License 2.0 , Project Creator : SnowVolf
/**
 * Add the SHA1 of every file to the manifest, creating it if necessary.
 */
private Manifest addDigestsToManifest(Map<String, ZioEntry> entries) throws IOException, GeneralSecurityException {
    Manifest input = null;
    ZioEntry manifestEntry = entries.get(JarFile.MANIFEST_NAME);
    if (manifestEntry != null) {
        input = new Manifest();
        input.read(manifestEntry.getInputStream());
    }
    Manifest output = new Manifest();
    Attributes main = output.getMainAttributes();
    if (input != null) {
        main.putAll(input.getMainAttributes());
    } else {
        main.putValue("Manifest-Version", "1.0");
        main.putValue("Created-By", "1.0 (Android SignApk)");
    }
    // BASE64Encoder base64 = new BASE64Encoder();
    MessageDigest md = MessageDigest.getInstance("SHA1");
    byte[] buffer = new byte[512];
    int num;
    // We sort the input entries by name, and add them to the
    // output manifest in sorted order.  We expect that the output
    // map will be deterministic.
    TreeMap<String, ZioEntry> byName = new TreeMap<String, ZioEntry>();
    byName.putAll(entries);
    boolean debug = getLogger().isDebugEnabled();
    if (debug)
        getLogger().debug("Manifest entries:");
    for (ZioEntry entry : byName.values()) {
        if (canceled)
            break;
        String name = entry.getName();
        if (debug)
            getLogger().debug(name);
        if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !name.equals(CERT_SF_NAME) && !name.equals(CERT_RSA_NAME) && (stripPattern == null || !stripPattern.matcher(name).matches())) {
            progressHelper.progress(ProgressEvent.PRORITY_NORMAL, resourceAdapter.getString(ResourceAdapter.Item.GENERATING_MANIFEST));
            InputStream data = entry.getInputStream();
            while ((num = data.read(buffer)) > 0) {
                md.update(buffer, 0, num);
            }
            Attributes attr = null;
            if (input != null) {
                sys.util.jar.Attributes inAttr = input.getAttributes(name);
                if (inAttr != null)
                    attr = new Attributes(inAttr);
            }
            if (attr == null)
                attr = new Attributes();
            attr.putValue("SHA1-Digest", Base64.encode(md.digest()));
            output.getEntries().put(name, attr);
        }
    }
    return output;
}