python签名脚本,同时支持v1和v2签名

2017-05-22  本文已影响1156人  Nestor_Gu
# -*- coding: utf-8 -*-
import os
import shutil
import os.path
import re

BASE_DIR = os.path.dirname(__file__)

out = os.path.exists('out')
if(out):
    shutil.rmtree('out')
    os.mkdir('out')
else:
    os.mkdir('out')

_build = os.path.exists('build')
if(_build):
    shutil.rmtree('build')
    os.mkdir('build')
else:
    os.mkdir('build')

keystore = '@@@@@@@@@'
keypass = '@@@@@@@@@'
keyalias = '@@@@@@@@@'

BASE_DIR = os.path.dirname(__file__)

outDir = os.path.join(BASE_DIR, "out")
buildDir = os.path.join(BASE_DIR, "build")
targetDir = os.path.join(BASE_DIR, "target")
list = os.listdir(os.path.join(BASE_DIR, "target"))
for file in list:
    print("start sign: " + file)
    signedFile = os.path.join(buildDir, file + "signed.apk")
    outFile = os.path.join(outDir, file)
    f = os.path.join(targetDir, file)

    # v1签名
    signcmd = 'jarsigner -sigalg SHA1withRSA -digestalg SHA1 -keystore "%s" -storepass "%s" -signedjar "%s" "%s" "%s"' % (keystore, keypass, signedFile, f, keyalias)
    os.system(signcmd)

    # zipalign
    aligncmd = 'zipalign -f 4 "%s" "%s"' % (signedFile, outFile)
    os.system(aligncmd)

    # v2签名
    signcmd2 = 'apksigner sign --ks %s --ks-pass pass:%s --ks-key-alias %s %s' % (keystore, keypass, keyalias, outFile)
    os.system(signcmd2)

    print(file + " finish\n")
--ks <filename>
The signer's private key and certificate chain reside in the given Java-based KeyStore file. If the filename is set to "NONE", the KeyStore containing the key and certificate doesn't need a file specified, which is the case for some PKCS #11 KeyStores.
--ks-key-alias <alias>
The name of the alias that represents the signer's private key and certificate data within the KeyStore. If the KeyStore associated with the signer contains multiple keys, you must specify this option.
--ks-pass <input-format>
The password for the KeyStore that contains the signer's private key and certificate. You must provide a password to open a KeyStore. The apksigner tool supports the following formats:

pass:<password> – Password provided inline with the rest of the apksigner sign command.
env:<name> – Password is stored in the given environment variable.
file:<filename> – Password is stored as a single line in the given file.
stdin – Password is provided as a single line in the standard input stream. This is the default behavior for --ks-pass.
Note: If you include multiple passwords in the same file, specify them on separate lines. The apksigner tool associates passwords with an APK's signers based on the order in which you specify the signers. If you've provided two passwords for a signer, apksigner interprets the first password as the KeyStore password and the second one as the key password.

https://developer.android.com/studio/command-line/apksigner.html

此为blog备份,原地址:http://blog.yzapp.cn/python签名脚本,同时支持v1和v2签名.html

上一篇 下一篇

猜你喜欢

热点阅读