-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApkCertificate.go
62 lines (57 loc) · 1.27 KB
/
ApkCertificate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package ApkInfGo
import (
"log"
"os/exec"
"strings"
)
type ApkCertSt struct {
CertMD5 string
CertSHA1 string
CertSHA256 string
SignAlgorithm string
FilePath string
}
type ConfCert struct {
keytool string
}
func ApkCertificate(keytoolApp string) *ConfCert {
c := &ConfCert{keytool: keytoolApp}
return c
}
func (c *ConfCert) File(apk string) *ApkCertSt {
out, err := exec.Command(c.keytool, "-list", "-printcert", "-jarfile", apk).Output()
if err != nil {
log.Printf("err: %q, file: %q", err, apk)
return nil
}
//log.Printf("apk file - %q\n", apk)
data := strings.Split(string(out), "\n")
cert := ApkCertSt{FilePath: apk}
for _, s := range data {
arr := strings.Split(s, ": ")
if len(arr) != 2 {
//log.Printf("error split - %q\n", s)
continue
}
arr[0] = strings.Trim(arr[0], " \t")
switch arr[0] {
case "MD5":
//log.Printf("MD5 - %q\n", arr[1])
cert.CertMD5 = strings.Trim(arr[1], " ")
break
case "SHA1":
//log.Printf("SHA1 - %q\n", arr[1])
cert.CertSHA1 = arr[1]
break
case "SHA256":
//log.Printf("SHA256 - %q\n", arr[1])
cert.CertSHA256 = arr[1]
break
case "Signature algorithm name":
//log.Printf("Signature algorithm name - %q\n", arr[1])
cert.SignAlgorithm = arr[1]
break
}
}
return &cert
}