Search in sources :

Example 1 with TinySignImpl

use of com.googlecode.d2j.signapk.TinySignImpl in project dex2jar by pxb1988.

the class ApkSign method doCommandLine.

@Override
protected void doCommandLine() throws Exception {
    if (remainingArgs.length != 1) {
        usage();
        return;
    }
    Path apkIn = new File(remainingArgs[0]).toPath();
    if (!Files.exists(apkIn)) {
        System.err.println(apkIn + " is not exists");
        usage();
        return;
    }
    if (output == null) {
        if (Files.isDirectory(apkIn)) {
            output = new File(apkIn.getFileName() + "-signed.apk").toPath();
        } else {
            output = new File(getBaseName(apkIn.getFileName().toString()) + "-signed.apk").toPath();
        }
    }
    if (Files.exists(output) && !forceOverwrite) {
        System.err.println(output + " exists, use --force to overwrite");
        usage();
        return;
    }
    Path tmp = null;
    try {
        final Path realJar;
        if (Files.isDirectory(apkIn)) {
            realJar = Files.createTempFile("d2j", ".jar");
            tmp = realJar;
            System.out.println("zipping " + apkIn + " -> " + realJar);
            try (FileSystem fs = createZip(realJar)) {
                final Path outRoot = fs.getPath("/");
                walkJarOrDir(apkIn, new FileVisitorX() {

                    @Override
                    public void visitFile(Path file, String relative) throws IOException {
                        Path target = outRoot.resolve(relative);
                        createParentDirectories(target);
                        Files.copy(file, target);
                    }
                });
            }
        } else {
            realJar = apkIn;
        }
        AbstractJarSign signer;
        if (tiny) {
            signer = new TinySignImpl();
        } else {
            try {
                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
                X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(ApkSign.class.getResourceAsStream("ApkSign.cer"));
                KeyFactory rSAKeyFactory = KeyFactory.getInstance("RSA");
                PrivateKey privateKey = rSAKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(ZipUtil.toByteArray(ApkSign.class.getResourceAsStream("ApkSign.private"))));
                signer = new SunJarSignImpl(cert, privateKey);
            } catch (Exception cnfe) {
                signer = new TinySignImpl();
            }
        }
        signer.sign(apkIn.toFile(), output.toFile());
        System.out.println("sign " + realJar + " -> " + output);
    } finally {
        if (tmp != null) {
            Files.deleteIfExists(tmp);
        }
    }
}
Also used : Path(java.nio.file.Path) SunJarSignImpl(com.googlecode.d2j.signapk.SunJarSignImpl) PrivateKey(java.security.PrivateKey) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) AbstractJarSign(com.googlecode.d2j.signapk.AbstractJarSign) FileSystem(java.nio.file.FileSystem) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) File(java.io.File) KeyFactory(java.security.KeyFactory) TinySignImpl(com.googlecode.d2j.signapk.TinySignImpl)

Aggregations

AbstractJarSign (com.googlecode.d2j.signapk.AbstractJarSign)1 SunJarSignImpl (com.googlecode.d2j.signapk.SunJarSignImpl)1 TinySignImpl (com.googlecode.d2j.signapk.TinySignImpl)1 File (java.io.File)1 IOException (java.io.IOException)1 FileSystem (java.nio.file.FileSystem)1 Path (java.nio.file.Path)1 KeyFactory (java.security.KeyFactory)1 PrivateKey (java.security.PrivateKey)1 CertificateFactory (java.security.cert.CertificateFactory)1 X509Certificate (java.security.cert.X509Certificate)1 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)1