get Jar Signature - Android File Input Output

Android examples for File Input Output:Jar File

Description

get Jar Signature

Demo Code

/*// ww w  .j  a  v a2s  .  co m
 * Copyright 2014-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static String getJarSignature(String packagePath)
            throws IOException {
        Pattern signatureFilePattern = Pattern
                .compile("META-INF/[A-Z]+\\.SF");

        ZipFile packageZip = null;
        try {
            packageZip = new ZipFile(packagePath);
            // For each file in the zip.
            for (ZipEntry entry : Collections.list(packageZip.entries())) {
                // Ignore non-signature files.
                if (!signatureFilePattern.matcher(entry.getName())
                        .matches()) {
                    continue;
                }

                BufferedReader sigContents = null;
                try {
                    sigContents = new BufferedReader(new InputStreamReader(
                            packageZip.getInputStream(entry)));
                    // For each line in the signature file.
                    while (true) {
                        String line = sigContents.readLine();
                        if (line == null || line.equals("")) {
                            throw new IllegalArgumentException(
                                    "Failed to find manifest digest in "
                                            + entry.getName());
                        }
                        String prefix = "SHA1-Digest-Manifest: ";
                        if (line.startsWith(prefix)) {
                            return line.substring(prefix.length());
                        }
                    }
                } finally {
                    if (sigContents != null) {
                        sigContents.close();
                    }
                }
            }
        } finally {
            if (packageZip != null) {
                packageZip.close();
            }
        }

        throw new IllegalArgumentException("Failed to find signature file.");
    }
}

Related Tutorials