mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
43 lines
1.3 KiB
Java
43 lines
1.3 KiB
Java
package minsecurity.certificate.X509;
|
|
|
|
import java.security.*;
|
|
import java.security.cert.CertificateException;
|
|
import java.security.cert.X509Certificate;
|
|
import java.util.Date;
|
|
|
|
/*
|
|
* @Author: hongyu guo
|
|
* @Description:
|
|
* @Version: 1.0.0
|
|
* @Date: 15:59 2021/03/05
|
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
|
*/
|
|
public class X509CertUtil {
|
|
|
|
public static boolean verify(X509Certificate certificate, PublicKey publicKey){
|
|
try {
|
|
certificate.verify(publicKey);
|
|
|
|
} catch (CertificateException | NoSuchAlgorithmException |
|
|
InvalidKeyException | NoSuchProviderException | SignatureException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static boolean isExpired(X509Certificate certificate){
|
|
Date notAfter = certificate.getNotAfter();
|
|
Date notBefore = certificate.getNotBefore();
|
|
long now = System.currentTimeMillis();
|
|
return now < notBefore.getTime() || now > notAfter.getTime();
|
|
}
|
|
|
|
public static boolean isValid(X509Certificate certificate, PublicKey publicKey){
|
|
boolean isExpired = isExpired(certificate);
|
|
boolean isVerify = verify(certificate, publicKey);
|
|
return !isExpired & isVerify;
|
|
}
|
|
|
|
}
|