Bu, dökümanın eski bir sürümüdür!
docs/samples altında xmlsig.samples.Detached 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, 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);
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));
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, new StreamResult(new FileOutputStream(SIGNATURE_FILENAME)));
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.
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.