golang 生成android 签名文件

zdglf · · 1613 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

package main

import (
    "strings"
    "log"
    "os"
    "archive/zip"
    "crypto/sha1"
    "io"
    "path"
    "encoding/base64"
)

func Sha1File(f string) error {
    sha1h := sha1.New()
    file, err:= os.Open(f)
    if err != nil{
        return err
    }
    defer file.Close()
    if _, err = io.Copy(sha1h,file);err!=nil{
        return err
    }
    log.Println("sha1:", base64.StdEncoding.EncodeToString(sha1h.Sum(nil)))
    return nil
}

func GenerateApkSignFile(apk_file, dic string) (err error){
    var reader *zip.ReadCloser
    if reader, err = zip.OpenReader(apk_file); err != nil{
        return
    }
    defer reader.Close()
    if err = os.MkdirAll(dic, 0755); err != nil{
        return
    }
    manifest_mf := path.Join(dic, "MANIFEST.MF")
    cert_sf := path.Join(dic, "CERT.SF")

    mm_header := "Manifest-Version: 1.0\nBuilt-By: Generated-by-ADT\nCreated-By: Android Gradle 2.2.2\n\n"

    mm_sha1 := sha1.New()

    var mm_file *os.File
    var cf_file *os.File

    if mm_file, err = os.Create(manifest_mf); err != nil{
        return
    }
    if cf_file, err = os.Create(cert_sf); err != nil{
        return
    }
    defer cf_file.Close()
    mm_file.Write([]byte(mm_header))
    mm_sha1.Write([]byte(mm_header))


    for _, file := range reader.File {
        if strings.HasPrefix(file.Name, "META-INF"){
            continue
        }
        if strings.HasSuffix(file.Name, "/"){
            continue
        }
        file_name := "Name: "+file.Name
        if len(file_name) <= 70{
            mm_file.Write([]byte(file_name+"\n"))
            mm_sha1.Write([]byte(file_name+"\n"))

        }else{
            mm_file.Write([]byte(file_name[0:70]+"\n"))
            mm_sha1.Write([]byte(file_name[0:70]+"\n"))


            mm_file.Write([]byte(" "+file_name[70:]+"\n"))
            mm_sha1.Write([]byte(" "+file_name[70:]+"\n"))

        }

        if rc, e := file.Open(); e != nil{
            err = e
            return
        }else{
            sha1h := sha1.New()
            if _, err = io.Copy(sha1h,rc);err!=nil{
                return
            }
            sha1_data := base64.StdEncoding.EncodeToString(sha1h.Sum(nil))
            mm_file.Write([]byte("SHA1-Digest: "+sha1_data+"\n\n"))
            mm_sha1.Write([]byte("SHA1-Digest: "+sha1_data+"\n\n"))

            rc.Close()
        }
    }
    mm_file.Close()
    mm_file, err = os.Open(manifest_mf)
    mm_file.Seek(int64(len(mm_header)), os.SEEK_SET)
    cf_header := "Signature-Version: 1.0\nX-Android-APK-Signed: 2\nSHA1-Digest-Manifest: "+base64.StdEncoding.EncodeToString(mm_sha1.Sum(nil))+"\nCreated-By: 1.0 (Android)\n\n"
    cf_file.Write([]byte(cf_header))
    io.Copy(cf_file, mm_file)
    mm_file.Close()
    return
}




func main() {
    err := GenerateApkSignFile(`C:\Users\Donggui\Documents\encrypted.apk`, `C:\Users\Donggui\Desktop\`)
    if err != nil{
        log.Println(err)
    }

}

有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:zdglf

查看原文:golang 生成android 签名文件

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1613 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传