Java 8 Certificate - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Certificate class.

This code example shows how to use the following methods:getPublicKey, getNotAfter, getNotBefore, getSerialNumber, getSigAlgName
 
		X509Certificate certificate = loadCertificate(certificatePath);
		 
		X509V3CertificateGenerator certGenerator = new X509V3CertificateGenerator();
 
		certGenerator.setSerialNumber(certificate.getSerialNumber());
		certGenerator.setPublicKey(certificate.getPublicKey());
		certGenerator.setSubjectDN(certificate.getSubjectX500Principal());
		certGenerator.setIssuerDN(issuerCertificate.getSubjectX500Principal());
		certGenerator.setNotBefore(certificate.getNotBefore());
		certGenerator.setNotAfter(certificate.getNotAfter());
		certGenerator.setSignatureAlgorithm(certificate.getSigAlgName());
 
		X509Certificate signedCertificate = certGenerator.generate(issuerPrivateKey);
 
		File file = new File(outPutCertificatePath);
		if (!file.exists()) {
			FileUtils.touch(file);
		} 
		 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getPublicKey, getSigAlgName, getSignature, getTBSCertificate
    private void doTest( 
        String      certName)
        throws Exception 
    { 
        X509Certificate  cert = loadCert(certName);
        byte[]           tbs = cert.getTBSCertificate();
        Signature        sig = Signature.getInstance(cert.getSigAlgName(), "BC");
         
        sig.initVerify(cert.getPublicKey());
         
        sig.update(tbs);
         
        assertFalse(sig.verify(cert.getSignature()));
    } 
 
    private X509Certificate loadCert(
        String certName)
        throws Exception 
    { 
        CertificateFactory rd = CertificateFactory.getInstance("X.509", "BC");
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getEncoded, getType
        DataOutputStream    dOut)
        throws IOException 
    { 
        try 
        { 
            byte[]      cEnc = cert.getEncoded();
 
            dOut.writeUTF(cert.getType());
            dOut.writeInt(cEnc.length);
            dOut.write(cEnc);
        } 
        catch (CertificateEncodingException ex)
        { 
            throw new IOException(ex.toString());
        } 
    } 
 
    private Certificate decodeCertificate(
        DataInputStream   dIn)
        throws IOException 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getEncoded, getIssuerDN, getIssuerX500Principal, getSerialNumber
             *  
             * XXX: not correct according to RFC 4514. 
             */ 
            // TODO: check if issuerName is different on getTBSCertificate 
            // issuerName = PrincipalUtil.getIssuerX509Principal(certificate).getName().replace(",", ", "); 
            issuerName = certificate.getIssuerDN().getName().replace(",", ", ");
        } else { 
            issuerName = certificate.getIssuerX500Principal().toString();
        } 
        issuerSerial.setX509IssuerName(issuerName);
        issuerSerial.setX509SerialNumber(certificate.getSerialNumber());
 
        byte[] encodedCertificate;
        try { 
            encodedCertificate = certificate.getEncoded();
        } catch (CertificateEncodingException e) {
            throw new RuntimeException("certificate encoding error: "
                    + e.getMessage(), e);
        } 
        DigestAlgAndValueType certDigest = certId.addNewCertDigest(); 
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getType, getIssuerDN, getNotAfter, getNotBefore, getSubjectDN
	} 
 
	public void list(String alias, Certificate cert) {
		if (cert instanceof X509Certificate) {
			X509Certificate x509 = (X509Certificate) cert;
			System.out.println(" " + alias + " (" + x509.getType() + ")\n" +
					"  valid from " + x509.getNotBefore() + " to " + x509.getNotAfter() + "\n" +
					"  for        " + x509.getSubjectDN().getName() + "\n" +
					"  issued by  " + x509.getIssuerDN().getName());
		} else { 
			System.out.println(" " + alias + " (" + cert.getType() + ")");
		} 
	} 
 
	public void print(Certificate c) {
		System.out.println(c.getType() + " " + c);
	} 
 
	abstract void run(File keystore, Map args) throws Exception;
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:verify, getIssuerX500Principal, getSubjectX500Principal
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getPublicKey, getIssuerDN, getNotAfter, getNotBefore, getSerialNumber
    } 
 
    private static CertData fillCertData(X509Certificate cert, Format formatter) throws CertificateEncodingException {
 
        CertData certData = new CertData();
        certData.setSubjectDN(cert.getSubjectDN().getName());
        certData.setIssuerDN(cert.getIssuerDN().getName());
        certData.setSerialNumber(cert.getSerialNumber());
        certData.setVersion(cert.getVersion());
        certData.setNotAfter(formatter.format(cert.getNotAfter()));
        certData.setNotBefore(formatter.format(cert.getNotBefore()));
        certData.setPublicKey(Base64.encode(cert.getPublicKey().getEncoded()));
        return certData;
    } 
} 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getEncoded, getSerialNumber
    } 
 
    static IssuerAndSerialNumber getIssuerAndSerialNumber(X509Certificate cert)
        throws CertificateEncodingException 
    { 
        Certificate certStruct = Certificate.getInstance(cert.getEncoded());
 
        return new IssuerAndSerialNumber(certStruct.getIssuer(), cert.getSerialNumber());
    } 
 
 
    static byte[] getSubjectKeyId(X509Certificate cert)
    { 
        byte[] ext = cert.getExtensionValue(X509Extension.subjectKeyIdentifier.getId());
 
        if (ext != null)
        { 
            return ASN1OctetString.getInstance(ASN1OctetString.getInstance(ext).getOctets()).getOctets();
        } 
        else 
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getPublicKey, getSigAlgName, getSignature, getTBSCertificate
This code example shows how to use the following methods:getEncoded
        Certificate other = (Certificate)o;
 
        try 
        { 
            byte[] b1 = this.getEncoded();
            byte[] b2 = other.getEncoded();
 
            return Arrays.areEqual(b1, b2);
        } 
        catch (CertificateEncodingException e)
        { 
            return false; 
        } 
    } 
     
    public synchronized int hashCode() 
    { 
        if (!hashValueSet)
        { 
            hashValue = calculateHashCode();
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Java 8 Certificate Questions & Answers:

See Code Examples for Java 8 Certificate Methods: