Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Created October 17, 2016 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SelvinPL/fe855fb4d12f08a8d6b3becffdc7c458 to your computer and use it in GitHub Desktop.
Save SelvinPL/fe855fb4d12f08a8d6b3becffdc7c458 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2007 The Android Open Source Project
*
* 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.
*/
import java.util.*;
import java.lang.*;
import java.io.*;
import java.security.cert.Certificate;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
class test
{
private static final String ANDROID_MANIFEST_FILENAME = "AndroidManifest.xml";
public static void main (String[] args) throws java.lang.Exception
{
JarFile jarFile = new JarFile("test.apk");
Enumeration<JarEntry> entries = jarFile.entries();
final Manifest manifest = jarFile.getManifest();
while (entries.hasMoreElements()) {
final JarEntry je = entries.nextElement();
if (je.isDirectory()) continue;
final String name = je.getName();
if (name.startsWith("META-INF/"))
continue;
if (ANDROID_MANIFEST_FILENAME.equals(name)) {
final Attributes attributes = manifest.getAttributes(name);
//pkg.manifestDigest = ManifestDigest.fromAttributes(attributes);
}
final Certificate[] certs = loadCertificates(jarFile, je);
System.out.println(" entry " + je.getName()
+ ": certs=" + certs[0] + " ("
+ (certs != null ? certs.length : 0) + ")");
}
}
private static Certificate[] loadCertificates(JarFile jarFile, JarEntry entry)
throws Exception {
InputStream is = null;
try {
// We must read the stream for the JarEntry to retrieve
// its certificates.
is = jarFile.getInputStream(entry);
readFullyIgnoringContents(is);
return entry.getCertificates();
} catch (IOException | RuntimeException e) {
throw new Exception("Failed reading " + entry.getName() + " in " + jarFile, e);
} finally {
is.close();
}
}
public static long readFullyIgnoringContents(InputStream in) throws IOException {
byte[] buffer = new byte[4096];
int n = 0;
int count = 0;
while ((n = in.read(buffer, 0, buffer.length)) != -1) {
count += n;
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment