使用GO语言验证证书的有效期
package main
import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"time"
)
func main() {
certPEM, err := ioutil.ReadFile("path/to/certificate.pem")
if err != nil {
fmt.Println("Error reading certificate file:", err)
return
}
block, _ := pem.Decode(certPEM)
if block == nil {
fmt.Println("Failed to parse certificate PEM")
return
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
fmt.Println("Error parsing certificate:", err)
return
}
fmt.Println("Certificate Expiry Date:", cert.NotAfter)
now := time.Now()
if now.Before(cert.NotBefore) {
fmt.Println("Certificate is not yet valid")
} else if now.After(cert.NotAfter) {
fmt.Println("Certificate has expired")
} else {
fmt.Println("Certificate is valid")
}
}