一行指令,获取APK签名摘要,versioncode等信息

 从最初的100行代码,缩减到目前的一半,除去注释什么的,代码缩减了一大半,

就是使用如下指令,在cmd窗口执行如下指令,将会返回签名摘要信息

keytool -printcert -jarfile xxx.apk# 最后是你APK的位置,好像zip压缩也可以

下面是以FacebookAPK进行的测试,返回的结果 

完整的代码如下,核对两个APK签名是否一致,以及获取APK versionCode信息,一样使用一条指令,把当前指令获取之后,使用.read()方法,将从字符串中获取目标信息,可以采用正则表达式获取 所需内容

import os
import sys
import io
import re

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

# summary
def get_apk_signature(path):
    try:
        output = os.popen("keytooL -printcert -jarfile %s" % path).read()

        pattern_md5 = re.compile(r"MD5:.*")
        pattern_sha = re.compile(r'SHA\d{1,3}:.*')

        return re.findall(pattern_md5, output), re.findall(pattern_sha, output)

    except Exception as e:
        print(e)

# return versioncode
def get_versionCode(apkpath):
    output = os.popen("aapt dump badging %s" % apkpath).read()

    match = re.compile(r"versionCode='(\d+)'")
    return re.findall(match, output)

# check version_code
def check_versioncode(apk_path, apk_path1):
    version_code = []
    if apk_path.endswith(".apk") and apk_path1.endswith(".apk"):
        version_code.append(get_versionCode(apk_path))
        version_code.append(get_versionCode(apk_path1))
    else:
        return "please input right apk path"
    """
       output = 1   can update
       output = -1  can't update
       """
    if eval(version_code[0][0]) == eval(version_code[1][0]):
        return 1
    else:
        return -1

def check_signature(apk_path, apk_path1):
    result = []
    result.append(get_apk_signature(apk_path))
    result.append(get_apk_signature(apk_path1))
    """
    output = 1   same signature
    output = -1  different signature
    """
    if result[0] == result[1]:
        return 1
    else:
        return -1
if __name__ == "__main__":
    apk_path = r"C:\Users\Desktop\com.apk"
    apk_path1 = r"C:\Users\Desktop\\com1.apk"
    print(check_signature(apk_path, apk_path1))
    print(check_versioncode(apk_path, apk_path1))

猜你喜欢

转载自blog.csdn.net/m0_37693335/article/details/81327645