ESYAE-imza Kütüphaneleri

User Tools

Site Tools


esya:xades:kod

Bu, dökümanın eski bir sürümüdür!


XAdES İmza Kullanım Örnekleri

* İmza Atma *

Detached

docs/samples altında xmlsig.samples.Detached 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) 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); 5.3.2 Enveloping docs/samples altında xmlsig.samples.Enveloping class'ına göz atın. create context with working dir Context context = new Context(BASE_DIR); create signature according to context, with default type (XADES_BES)

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)); 5.3.3 Enveloped docs/samples altında xmlsig.samples.Enveloped class'ına göz atın. 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, with default type (XADES_BES) XMLSignature signature = new XMLSignature(context, SignatureType.XAdES_BES, false); attach signature to envelope envelopeDoc.getDocumentElement().appendChild(signature.getElement()); add document as reference, signature.addDocument(”#data1”, “text/xml”, false); add certificate to show who signed the document signature.addKeyInfo(CERTIFICATE); now sign it by using private key signature.sign(PRIVATE_KEY); output xml, 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, ESYA API Kullanım Kılavuzu 84 TÜBĠTAK BĠLGEM new StreamResult(new FileOutputStream(SIGNATURE_FILENAME))); 5.4 İmza Doğrulama Basit bir doğrulama kod örneği. Bu örnekte imza dosyadan yüklenmekte ve imza içerisinde yer alan imzalama sertifikası kullanılarak doğrulanmaktadır. XMLSignature signature = XMLSignature.parse( new FileDocument(new File(FILE_NAME)), new Context(BASE_DIR)) ; no params, use the certificate in key info ValidationResult result = signature.verify(); 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. 5.5 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. tr.gov.tubitak.uekae.esya.api.smartcard.util. SCSignerWithCertSerialNo ve tr.gov.tubitak.uekae.esya.api.smartcard.util. SCSignerWithKeyLabel sınıfları sizin için BaseSigner arayüzünü implement etmektedir. Örnek kod için xmlsig.samples.SmartCardSigner class'ına bakabilirsiniz: public BaseSigner getCardSigner() throws Exception { SmartCard smartCard = new SmartCard(CardType.AKIS); long sessionID = -1; long slot = 0; slot = smartCard.getTokenPresentSlotList()[0]; sessionID = smartCard.openSession(slot); use label or serial number to read certificate List<byte[]> value = smartCard.readCertificate(sessionID, “bilen.ogreten#work.netSIGN0”); SMARTCARD_PUBLIC_CERTIFICATE = new ECertificate(value.get(0)); login now to use private certificate for signing later smartCard.login(sessionID, “12345”); create a signer by using serial number of label return new SCSignerWithCertSerialNo(smartCard, sessionID, slot, SMARTCARD_PUBLIC_CERTIFICATE.getSerialNumber().toByteArray(), SignatureAlg.RSA_SHA256.getName()); ESYA API Kullanım Kılavuzu 85 TÜBĠTAK BĠLGEM } sample usage { BaseSigner signer = getCardSigner(); signature.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256); add certificate to show who signed the document signature.addKeyInfo(SMARTCARD_PUBLIC_CERTIFICATE); now sign it by using smart card signature.sign(signer);

esya/xades/kod.1374229947.txt.gz · Son değiştirilme: 2013/07/19 10:32 Değiştiren: Ahmet Yetgin