Bu sayfanın seçili sürümü ile mevcut sürümü arasındaki farkları gösterir.
esya:xades:kod-imza-atma-dogrulama [2013/07/19 10:59] Ahmet Yetgin created |
esya:xades:kod-imza-atma-dogrulama [2013/08/20 11:13] (mevcut) Dindar Öz |
||
---|---|---|---|
Satır 1: | Satır 1: | ||
====== XAdES İmza Kullanım Örnekleri ====== | ====== XAdES İmza Kullanım Örnekleri ====== | ||
+ | |||
+ | Örnek kodları ''tr.gov.tubitak.uekae.esya.api.xades.example'' paketi altında bulabilirsiniz. | ||
===== İmza Atma ===== | ===== İmza Atma ===== | ||
- | ==== Ayrık (Detached) İmza ==== | + | ''formats'' altında ''BES'' sınıfındaki kod ile örnek bir imza atabilirsiniz. |
- | + | ||
- | docs/samples altında xmlsig.samples.Detached class'ına göz atın. | + | |
<sxh java> | <sxh java> | ||
- | // create context with working dir | + | // create context with working directory |
- | Context context = new Context(BASE_DIR); | + | Context context = createContext(); |
- | // create signature according to context, | + | // create signature according to context, |
- | // with default type (XADES_BES) | + | |
- | XMLSignature signature = new XMLSignature(context); | + | |
- | + | ||
- | // add document as reference, but do not embed it | + | |
- | // into the signature (embed=false) | + | |
- | signature.addDocument("./sample.txt", "text/plain", false); | + | |
- | + | ||
- | // add certificate to show who signed the document | + | |
- | signature.addKeyInfo(CERTIFICATE); | + | |
- | + | ||
- | // now sign it by using private key | + | |
- | signature.sign(PRIVATE_KEY); | + | |
- | </sxh> | + | |
- | + | ||
- | ==== Zaflayan (Enveloping) İmza ==== | + | |
- | + | ||
- | docs/samples altında xmlsig.samples.Enveloping class'ına göz atın. | + | |
- | + | ||
- | <sxh java> | + | |
- | // create context with working dir | + | |
- | Context context = new Context(BASE_DIR); | + | |
- | + | ||
- | // create signature according to context, | + | |
// with default type (XADES_BES) | // with default type (XADES_BES) | ||
- | XMLSignature signature = new XMLSignature(context); | + | XMLSignature signature = new XMLSignature(context); |
- | + | ||
- | // add document as reference, and keep BASE64 version of data | + | |
- | // in an <Object tag, in a way that reference points to | + | |
- | // that <Object | + | |
- | // (embed=true) | + | |
- | signature.addDocument("./sample.txt", "text/plain", true); | + | |
- | + | ||
- | // add certificate to show who signed the document | + | |
- | signature.addKeyInfo(CERTIFICATE); | + | |
- | + | ||
- | // now sign it by using private key | + | |
- | signature.sign(PRIVATE_KEY); | + | |
- | signature.write(new FileOutputStream(SIGNATURE_FILENAME)); | + | |
- | </sxh> | + | |
- | + | ||
- | ==== Zarflanmış (Enveloped) İmza ==== | + | |
- | + | ||
- | docs/samples altında xmlsig.samples.Enveloped class'ına göz atın. | + | |
- | + | ||
- | <sxh java> | + | |
- | // here is our custom envelope xml | + | |
- | org.w3c.dom.Document envelopeDoc = new Envelope(); | + | |
- | + | ||
- | // create context with working dir | + | |
- | Context context = new Context(BASE_DIR); | + | |
- | + | ||
- | // define where signature belongs to | + | |
- | context.setDocument(envelopeDoc); | + | |
- | // create signature according to context, | + | // add document as reference, but do not embed it |
- | // with default type (XADES_BES) | + | // into the signature (embed=false) |
- | XMLSignature signature = new XMLSignature(context, SignatureType.XAdES_BES, false); | + | signature.addDocument("./sample.txt", "text/plain", false); |
- | // attach signature to envelope | + | signature.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256); |
- | envelopeDoc.getDocumentElement().appendChild(signature.getElement()); | + | |
- | // add document as reference, | + | // false-true gets non-qualified certificates while true-false gets qualified ones |
- | signature.addDocument("#data1", "text/xml", false); | + | X509Certificate cert = JSmartCardManager.getInstance().getSignatureCertificate(true, false); |
- | // add certificate to show who signed the document | + | // add certificate to show who signed the document |
- | signature.addKeyInfo(CERTIFICATE); | + | signature.addKeyInfo(new ECertificate(cert.getEncoded())); |
- | // now sign it by using private key | + | // now sign it by using smart card |
- | signature.sign(PRIVATE_KEY); | + | signature.sign(JSmartCardManager.getInstance().getSigner(PIN, cert)); |
- | // output xml, | + | signature.write(new FileOutputStream(BASE_DIR + SIGNATURE_FILENAME)); |
- | // this time we dont use signature.write because we need to write | + | |
- | // whole document instead of signature | + | |
- | Source source = new DOMSource(envelopeDoc); | + | |
- | Transformer transformer = TransformerFactory.newInstance().newTransformer(); | + | |
- | // write to file | + | |
- | transformer.transform(source, new StreamResult(new FileOutputStream(SIGNATURE_FILENAME))); | + | |
</sxh> | </sxh> | ||
Satır 101: | Satır 43: | ||
</sxh> | </sxh> | ||
- | Bu örnekte yalnız ana imzanın doğrulaması yapıldığına dikkat edilmelidir. Seri imza doğrulaması da yapan örnek kod için xmlsig.samples.Validation sınıfına göz atabilirsiniz. | + | Bu örnekte yalnız ana imzanın doğrulaması yapıldığına dikkat edilmelidir. Seri imza doğrulaması da yapan örnek kod için ''validation.Validation'' sınıfına göz atabilirsiniz. Yine aynı sınıftaki ''validateParallel'' fonksiyonu ile de paralel imzaların doğrulamasını gerçekleştirebilirsiniz. |
===== Akıllı Kart İşlemleri ===== | ===== Akıllı Kart İşlemleri ===== | ||
- | Akıllı kart kütüphanesinin detaylı kullanımı için akıllı kart kullanım kılavuzuna göz atınız. Akılı kart kullanarak imzalama yapmak için uygun BaseSigner sınıfı oluşturmalısınız. Gizli anahtar kart içinde bulunduğu için imza kütüphanesine parametre oarak verilemez. Kütüphane ile dağıtılan ''SmartCardManager'' sınıfı başlangıç için yeterli seviyede akıllı kart ihtiyaçlarını görmektedir. | + | Akıllı kart kütüphanesinin detaylı kullanımı için akıllı kart kullanım kılavuzuna göz atınız. Akılı kart kullanarak imzalama yapmak için uygun BaseSigner sınıfı oluşturmalısınız. Gizli anahtar kart içinde bulunduğu için imza kütüphanesine parametre olarak verilemez. Kütüphane ile dağıtılan ''SmartCardManager'' sınıfı başlangıç için yeterli seviyede akıllı kart ihtiyaçlarını görmektedir. |