Skip to content

Instantly share code, notes, and snippets.

@petervalencic
Last active November 28, 2023 18:34
Show Gist options
  • Save petervalencic/ad5af7800770c2c04eec0e0aa6c779b7 to your computer and use it in GitHub Desktop.
Save petervalencic/ad5af7800770c2c04eec0e0aa6c779b7 to your computer and use it in GitHub Desktop.
FURS podpis xml računa
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.*;
import javax.xml.XMLConstants;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dsig.dom.DOMSignContext;
import javax.xml.crypto.dsig.keyinfo.*;
import javax.xml.crypto.dsig.spec.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class SignFileExample {
public static void main(String[] args) throws Exception {
String xml = "Tukaj vnesite vaš xml ";
String outputFile = "c:/podpisan.xml";
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(new ByteArrayInputStream(xml.getBytes()));
Node node = doc.getElementsByTagName("fu:BusinessPremiseRequest").item(0);
KeyStore p12 = KeyStore.getInstance("pkcs12");
p12.load(new FileInputStream("c:/cert/certifikatfursa.p12"), "GESLO".toCharArray());
Enumeration<String> aliases = p12.aliases();
String alias = aliases.nextElement();
System.out.println("Alias certifikata:" + alias);
Key privateKey = p12.getKey(alias, "GESLO".toCharArray());
KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) p12.getEntry(alias,
new KeyStore.PasswordProtection("GESLO".toCharArray()));
X509Certificate cert = (X509Certificate) keyEntry.getCertificate();
PublicKey publicKey = cert.getPublicKey();
final XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance("DOM");
Reference ref = sigFactory.newReference("#data",
sigFactory.newDigestMethod(DigestMethod.SHA256, null),
Collections.singletonList(sigFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)),
null,
null);
SignedInfo si = sigFactory.newSignedInfo(sigFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null),
sigFactory.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null),
Collections.singletonList(ref));
KeyInfoFactory keyInfoFactory = sigFactory.getKeyInfoFactory();
X509IssuerSerial x509IssuerSerial = keyInfoFactory.newX509IssuerSerial(cert.getSubjectX500Principal().getName(),
cert.getSerialNumber());
List<Object> x509Content = new ArrayList<>();
x509Content.add(cert.getSubjectX500Principal().getName());
x509Content.add(x509IssuerSerial);
KeyValue keyValue = keyInfoFactory.newKeyValue(publicKey);
X509Data xd = keyInfoFactory.newX509Data(x509Content);
KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(xd));
DOMSignContext dsc = new DOMSignContext(privateKey, node);
XMLSignature signature = sigFactory.newXMLSignature(si, keyInfo);
signature.sign(dsc);
try (OutputStream os = new FileOutputStream(outputFile)) {
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(os));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment