Utility class to sign xml width XAdES-BES (XML Advanced Electronic Signatures) digital signature.
String xml = "<document><id>1</id></document>";
XAdESSigner signer = new XAdESSigner("keystore.jks", "password", "selfsigned");
String signed = signer.sign(xml);
X509Certificate cert = signer.validate(signed);
Output:
<document>
<id>1</id>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-f500d752-2926-4c0b-ab78-bf039dc47e54">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2006/12/xml-c14n11#WithComments"/>
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<Transform Algorithm="http://www.w3.org/2006/12/xml-c14n11#WithComments"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>zPneqKXvQX7LCLt38TtAHsw3IJSMwPObQgVCqv5ENbY=</DigestValue>
</Reference>
<Reference URI="#QualifyingProperties_ID">
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>dELwQV/Re3WROkb1FZqEsUU1OgoG4bDclquMhB4pEqU=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>
rbde0iq+wsPIVgEPgCu4sWK1wYikG3/A1hnT/X2ensvmQmFjrBrvbjwuF8hHOBq3BWQ1InTZiFPO
3pIf0UdeblV0f6w0tX6jM5gIFotieoKboHSJOtwU3TdwpXRWtroxqtxKw2YbFQT4CkCdFKxqAMNU
zMjniSt0nHVqXSw8DN74uCQBFC7wREfk/jo9ZVX3qQLLKwvd5lMSRpCqDzc3SqYS9G+XxTfwM/XL
mfVVNXiO8vASIjcIuDvxVvn6FFztFaxrUpASEnikbFTmAqbn12zL8x2k8JxWQ1PRwYEq7URDYx5V
EwoFHRArb2ZlX9O3MpLW87BVrDMjvaD+CYCHkg==
</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>
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Vv+jKubfdl
G5ydx/oib3D2IMgk2BUf2lDMU4fvU3LdpMPkD9kDt/dY13PDMuTIeDMOdA==
</X509Certificate>
</X509Data>
</KeyInfo>
<Object>
<QualifyingProperties Id="QualifyingProperties_ID">
<SignedProperties>
<SignedSignatureProperties>
<SigningTime>2021-01-28T13:35:58Z</SigningTime>
</SignedSignatureProperties>
</SignedProperties>
</QualifyingProperties>
</Object>
</Signature>
</document>
git clone https://github.com/giosil/xades-bes.git
mvn clean install