24.12.2014 Views

Download - Svetlin Nakov

Download - Svetlin Nakov

Download - Svetlin Nakov

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

InputStream aCertStream)<br />

throws GeneralSecurityException {<br />

CertificateFactory cf=CertificateFactory.getInstance(X509_CERTIFICATE_TYPE);<br />

X509Certificate cert = (X509Certificate)cf.generateCertificate(aCertStream);<br />

return cert;<br />

}<br />

/**<br />

* Loads X.509 certificate from DER-encoded binary file (.CER file).<br />

*/<br />

public static X509Certificate loadX509CertificateFromCERFile(String aFileName)<br />

throws GeneralSecurityException, IOException {<br />

FileInputStream fis = new FileInputStream(aFileName);<br />

X509Certificate cert = null;<br />

try {<br />

cert = loadX509CertificateFromStream(fis);<br />

} finally {<br />

fis.close();<br />

}<br />

return cert;<br />

}<br />

/**<br />

* Loads a certification chain from given Base64-encoded string, containing<br />

* ASN.1 DER formatted chain, stored with PkiPath encoding.<br />

*/<br />

public static CertPath loadCertPathFromBase64String(<br />

String aCertChainBase64Encoded)<br />

throws CertificateException, IOException {<br />

byte[] certChainEncoded = Base64Utils.base64Decode(aCertChainBase64Encoded);<br />

CertificateFactory cf=CertificateFactory.getInstance(X509_CERTIFICATE_TYPE);<br />

InputStream certChainStream = new ByteArrayInputStream(certChainEncoded);<br />

CertPath certPath;<br />

try {<br />

certPath = cf.generateCertPath(certChainStream, CERT_CHAIN_ENCODING);<br />

} finally {<br />

certChainStream.close();<br />

}<br />

return certPath;<br />

}<br />

/**<br />

* Verifies given digital singature. Checks if given signature is obtained by<br />

* signing given document with the private key corresponing to given public key.<br />

*/<br />

public static boolean verifyDocumentSignature(byte[] aDocument,<br />

PublicKey aPublicKey, byte[] aSignature)<br />

throws GeneralSecurityException {<br />

Signature signatureAlgorithm =<br />

Signature.getInstance(DIGITAL_SIGNATURE_ALGORITHM_NAME);<br />

signatureAlgorithm.initVerify(aPublicKey);<br />

signatureAlgorithm.update(aDocument);<br />

boolean valid = signatureAlgorithm.verify(aSignature);<br />

return valid;<br />

}<br />

/**<br />

* Verifies given digital singature. Checks if given signature is obtained<br />

* by signing given document with the private key corresponing to given<br />

* certificate.<br />

*/<br />

public static boolean verifyDocumentSignature(byte[] aDocument,<br />

X509Certificate aCertificate, byte[] aSignature)<br />

throws GeneralSecurityException {<br />

128

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!