-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadX509Cert.go
More file actions
40 lines (35 loc) · 789 Bytes
/
readX509Cert.go
File metadata and controls
40 lines (35 loc) · 789 Bytes
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
/*
读取X.509证书
*/
package main
import (
"crypto/x509"
"fmt"
"os"
)
func main() {
certCerFile, err := os.Open("./ca/jun.ming.wang.cer")
checkError(err)
derBytes := make([]byte, 1000) //比证书文件大
count, err := certCerFile.Read(derBytes)
checkError(err)
certCerFile.Close()
//截取到证书实际长度
cert, err := x509.ParseCertificate(derBytes[0:count])
checkError(err)
fmt.Println("Name %s\n", cert.Subject.CommonName)
fmt.Println("Not before %s\n", cert.NotBefore.String())
fmt.Println("Not after %s\n", cert.NotAfter.String())
}
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}
/*
output:
Name jun.ming.wang
Not before 2021-02-03 06:47:17 +0000 UTC
Not after 2022-02-03 06:47:17 +0000 UTC
*/