当你发布了应用的release版的apk,又想看这个apk文件的SHA1值,因为一些开放SDK(例如百度地图、谷歌地图)可能需要SHA1值生成对应key值来验证。本文介绍下在Linux上如何查看apk文件的SHA1值。
编写Shell脚本
新建一个脚本sha1.sh,脚本内容如下:
#!/bin/bash get_signature() { path=`jar tf "$1" | grep RSA` jar xf $1 $path keytool -printcert -file $path rm -r $path } mypath=`pwd` filepath="" if [ -d .temp_for_certificate ] then echo ".temp_for_certificate is exist,remove it first!" exit fi mkdir .temp_for_certificate cd .temp_for_certificate count=0 while [ -n "$1" ] do if [ `expr substr "$1" 1 1` = "/" ] #绝对路径 then filepath="$1" else #相对路径 filepath="$mypath/$1" fi if [ -d $filepath ] then all_apk=`find $filepath -name "*.apk"` for apk_path in $all_apk do count=$[$count+1] echo "(#$count) "`basename "$apk_path"`":" filepath=$apk_path get_signature "$filepath" echo "-----------------------------------------------------------" done else count=$[$count+1] echo "(#$count) "`basename "$1"`":" get_signature "$filepath" echo "-----------------------------------------------------------" fi shift done cd .. rm -r .temp_for_certificate echo "done!"
赋予执行权限:
chmod +x sha1.sh
安装依赖
当你执行脚本时,会提示你安装依赖(如何你本机没有java环境的话),运行以下命令安装依赖:
# Ubuntu apt install openjdk-11-jre-headless apt install openjdk-11-jdk-headless
执行脚本查寻SHA1值
执行脚本查看apk问及那的SHA1值:
sh sha1.sh apkname.apk
运行结果如下:
相关文章