Created
March 10, 2025 08:36
-
-
Save filip505/440715f592b04e4ed2ed177d12e257a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Demo{ | |
fun createInvoice(alias: String) { | |
val racunZahtjev = RacunZahtjev() | |
val id = "20d30b94-7da0-4c50-976d-bac6ab1f639a" | |
racunZahtjev.id = id | |
val idPoruke = UUID.randomUUID().toString() | |
val datumVrijeme = "24.01.2020T09:59:43" | |
racunZahtjev.zaglavlje = ZaglavljeType().also { | |
it.idPoruke = idPoruke | |
it.datumVrijeme = datumVrijeme | |
} | |
racunZahtjev.racun = RacunType().also { | |
it.oib = "98765432198" | |
it.isUSustPdv = true | |
it.datVrijeme = "24.01.2020T09:59:43" | |
it.oznSlijed = OznakaSlijednostiType.P | |
} | |
racunZahtjev.racun.brRac = BrojRacunaType().also { | |
it.brOznRac = "123456789" | |
it.oznNapUr = "12" | |
it.oznPosPr = "POSL1" | |
} | |
racunZahtjev.racun.pdv = PdvType().also { | |
it.porez.add(PorezType().also { | |
it.stopa = "25" | |
it.osnovica = "10" | |
it.iznos = "2.5" | |
}) | |
} | |
racunZahtjev.racun.iznosUkupno = "30" | |
racunZahtjev.racun.nacinPlac = NacinPlacanjaType.K | |
racunZahtjev.racun.oibOper = "01234567890" | |
racunZahtjev.racun.zastKod = "e4d909c290d0fb1ca068ffaddf22cbd0" | |
racunZahtjev.racun.isNakDost = false | |
racunZahtjev.racun.paragonBrRac = "123/458/5" | |
try { | |
Init.init() | |
// 2. Učitavanje certifikata | |
val fis = this::class.java.getResourceAsStream("/3120026.1.F1.p12") | |
?: throw FileNotFoundException("Certificate file not found: /3120026.1.F1.p12") | |
val keyStore = KeyStore.getInstance("PKCS12") | |
keyStore.load(fis, "finhir-sezwYr-8pisxo".toCharArray()) | |
val privateKey = keyStore.getKey(alias, "finhir-sezwYr-8pisxo".toCharArray()) as PrivateKey | |
val certificate = keyStore.getCertificate(alias) as X509Certificate | |
// 3. Serijalizacija u XML | |
val jaxbContext = JAXBContext.newInstance(RacunZahtjev::class.java) | |
val marshaller = jaxbContext.createMarshaller() | |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true) | |
val writer = StringWriter() | |
marshaller.marshal(racunZahtjev, writer) | |
val xmlString = writer.toString() | |
// 4. Kanonski XML | |
val dbf = DocumentBuilderFactory.newInstance() | |
dbf.isNamespaceAware = true | |
val db = dbf.newDocumentBuilder() | |
val inputSource = InputSource(StringReader(xmlString)).apply { encoding = "UTF-8" } | |
val document = db.parse(inputSource) | |
val canon = Canonicalizer.getInstance(CanonicalizationMethod.INCLUSIVE) | |
val rootNode: Node = document.documentElement | |
val outputStream = ByteArrayOutputStream() | |
canon.canonicalizeSubtree(rootNode, outputStream) | |
val canonicalizedXml = outputStream.toByteArray() | |
// 5. Potpisivanje | |
val fac = XMLSignatureFactory.getInstance("DOM") | |
val ref = fac.newReference( | |
"#" + id, | |
fac.newDigestMethod(DigestMethod.SHA256, null), | |
Collections.singletonList(fac.newTransform(Transform.ENVELOPED, null as TransformParameterSpec?)), | |
null, | |
null | |
) | |
val si = fac.newSignedInfo( | |
fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, null as C14NMethodParameterSpec?), | |
fac.newSignatureMethod(SignatureMethod.RSA_SHA256, null), | |
Collections.singletonList(ref) | |
) | |
val kif = fac.keyInfoFactory | |
val xd = kif.newX509Data(Collections.singletonList(certificate)) | |
val ki = kif.newKeyInfo(Collections.singletonList(xd)) | |
val signature = fac.newXMLSignature(si, ki) | |
//Create a new document from the canonicalized xml. | |
val canonicalizedDocument = db.parse(ByteArrayInputStream(canonicalizedXml)) | |
val dsc = DOMSignContext(privateKey, canonicalizedDocument.documentElement) | |
val root = canonicalizedDocument.documentElement | |
root.setIdAttribute("Id", true) | |
signature.sign(dsc) | |
// 6. Dodavanje potpisa u XML | |
val signatureElement = canonicalizedDocument.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature").item(0) as Element | |
canonicalizedDocument.documentElement.appendChild(signatureElement) | |
// 7. Serijalizacija potpisanog XML-a | |
val tf = TransformerFactory.newInstance() | |
val trans = tf.newTransformer() | |
val sw = StringWriter() | |
trans.transform(DOMSource(canonicalizedDocument), StreamResult(sw)) | |
val signedXmlString = sw.toString() | |
val unmarshaller = jaxbContext.createUnmarshaller() | |
val stringReader = StringReader(signedXmlString) | |
val signedRacunZahtjev = unmarshaller.unmarshal(stringReader) as RacunZahtjev | |
val fiskalizacijaPortType = FiskalizacijaService().fiskalizacijaPortType | |
val res = fiskalizacijaPortType.racuni(signedRacunZahtjev) | |
//val res = FiskalizacijaService().fiskalizacijaPortType.echo("bok") | |
val t = res | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment