{"id":18485311,"url":"https://github.com/arturopala/scala-xml-security","last_synced_at":"2025-04-08T19:32:22.869Z","repository":{"id":144518135,"uuid":"64325433","full_name":"arturopala/scala-xml-security","owner":"arturopala","description":"XML Security library for Scala - handy tool for easy signing, verifying, encrypting and decrypting XML documents","archived":false,"fork":false,"pushed_at":"2017-10-25T17:06:50.000Z","size":59,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T17:45:48.527Z","etag":null,"topics":["decryption","dom","encryption","scala","xml-document","xml-signature"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arturopala.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-27T16:41:46.000Z","updated_at":"2021-09-03T17:37:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"a5bfce02-aee4-4507-b547-479d4b650f69","html_url":"https://github.com/arturopala/scala-xml-security","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturopala%2Fscala-xml-security","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturopala%2Fscala-xml-security/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturopala%2Fscala-xml-security/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturopala%2Fscala-xml-security/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arturopala","download_url":"https://codeload.github.com/arturopala/scala-xml-security/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247912724,"owners_count":21017038,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decryption","dom","encryption","scala","xml-document","xml-signature"],"created_at":"2024-11-06T12:44:48.078Z","updated_at":"2025-04-08T19:32:22.864Z","avatar_url":"https://github.com/arturopala.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"Scala XML Security\n==================\n\n[![Build Status](https://semaphoreci.com/api/v1/arturopala/scala-xml-security/branches/master/badge.svg)](https://semaphoreci.com/arturopala/scala-xml-security)\n\nDealing with XML signatures or encryption requires usually a lot of magic config code followed by copy-pasted boilerplate. This small library wraps for you all the necessary configuration and logic to finally expose simple but essential API.\n\nMain Features\n=============\n\n-   **signature** creation and validation, \n-   **encryption** of XML document, \n-   **decryption** of XML document\n\nInstallation\n============\nAdd the following line to your project description in `build.sbt`:\n```\nlibraryDependencies += \"com.github.arturopala\" % \"scala-xml-security_2.12\" % \"1.2.0\"\n```\n\nYou can find available versions here:\n\n[http://search.maven.org/#search|ga|1|scala-xml-security](http://search.maven.org/#search%7Cga%7C1%7Cscala-xml-security)\n\nDependencies\n------------\nThis library brings into your project few transitive dependencies:\n-   org.apache.santuario:xmlsec - [Apache XML Security utils](http://santuario.apache.org/javaindex.html)\n-   org.bouncycastle:bcprov-jdk15on - [Cryptography provider](https://www.bouncycastle.org/)\n-   org.bouncycastle:bcpkix-jdk15on - [PKI support](https://www.bouncycastle.org/)\n-   org.json4s:json4s-native - [Scala JSON library](https://github.com/json4s/json4s)\n-   commons-codec:commons-codec - [Apache Commons Codec](https://commons.apache.org/proper/commons-codec/)\n\nUsage\n=====\n\nParse XML as a Document (DOM)\n-------------------------\nAPI:\n```scala\ndef parseDocument(document: String): Try[Document]\n```\nExample:\n```scala\nimport scala.util.Try\nimport org.w3c.dom.Document\nimport com.github.arturopala.xmlsecurity.XmlUtils\n\nval xml: String = ??? //some XML string\nval dom: Try[Document] = XmlUtils.parseDocument(xml)\n```\n\nLoad schema and validate XML document\n---------------------\nAPI:\n```scala\ndef loadSchema(schemaUrl: URL*): Try[Schema]\ndef validateDocument(schema: Schema)(dom: Document): Try[Document]\n```\nExample:\n```scala\nimport java.net.URL\nimport scala.util.Try\nimport org.w3c.dom.Document\nimport com.github.arturopala.xmlsecurity.XmlUtils\n\ndef getResource(r: String): URL = classOf[Document].getResource(r)\nval xml: String = ??? //some XML string\nval document: Try[Document] = for {\n  schema    \u003c- XmlUtils.loadSchema(\n                  getResource(\"/saml-schema-protocol-2.0.xsd\"), // relevant schemas\n                  getResource(\"/saml-schema-assertion-2.0.xsd\"),\n                  getResource(\"/xenc-schema.xsd\"),\n                  getResource(\"/xmldsig-core-schema.xsd\")\n               )\n  dom       \u003c- XmlUtils.parseDocument(xml)\n  validated \u003c- XmlUtils.validateDocument(schema)(dom)\n} yield validated\n```\n\nSign XML document\n-----------------\nAPI:\n```scala\ndef signDocument(\n    signatureAlgorithm: String,\n    digestAlgorithm:    String,\n    privateKey:         PrivateKey,\n    publicKey:          Option[PublicKey] = None)(dom: Document): Try[Document]\n    \ndef signDocument(\n    signatureAlgorithm: String,\n    digestAlgorithm:    String,\n    privateKey:         PrivateKey,\n    cert:               X509Certificate)(dom: Document): Try[Document]\n```\nExample:\n```scala\nimport java.security.KeyPair\nimport javax.security.cert.X509Certificate\nimport scala.util.Try\nimport org.w3c.dom.Document\nimport com.github.arturopala.xmlsecurity.{XmlUtils,XmlSecurity}\n\nval keyPair: KeyPair = ???\nval cerificate: X509Certificate = ???\nval xml: String = ??? //some XML string\nval document: Try[Document] = for {\n  dom     \u003c- XmlUtils.parseDocument(xml)\n  signed  \u003c- XmlSecurity.signDocument(\n                \"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256\",\n                \"http://www.w3.org/2001/04/xmlenc#sha256\",\n                keyPair.getPrivate,\n                certificate\n             )(dom)\n} yield signed\n```\n\nValidate XML signature\n----------------------\nAPI:\n```scala\ndef validateSignature: Document =\u003e Try[Document]\ndef validateSignature(publicKey: PublicKey): Document =\u003e Try[Document]\ndef validateSignature(keySelector: KeySelector)(dom: Document): Try[Document]\n```\nExample:\n```scala\nimport scala.util.Try\nimport org.w3c.dom.Document\nimport com.github.arturopala.xmlsecurity.{XmlUtils,XmlSecurity}\n\nval xml: String = ??? // some signed XML string\nval document: Try[Document] = for {\n  dom    \u003c- XmlUtils.parseDocument(xml)\n  valid  \u003c- XmlSecurity.validateSignature(dom)\n} yield valid\n```\n\nEncrypt XML document\n--------------------\nAPI:\n```scala\ndef encryptDocument(\n    cert:                X509Certificate,\n    encryptionAlgorithm: String,\n    keyWrapAlgorithm:    String,\n    digestAlgorithm:     String,\n    mgfAlgorithm:        String          = null,\n    oaepParams:          Array[Byte]     = null)(dom: Document): Try[Document]\n```\nExample:\n```scala\nimport scala.util.Try\nimport org.w3c.dom.Document\nimport javax.security.cert.X509Certificate\nimport com.github.arturopala.xmlsecurity.{XmlUtils,XmlSecurity}\n\nval cerificate: X509Certificate = ???\nval xml: String = ??? // some XML string\nval document: Try[Document] = for {\n  dom       \u003c- XmlUtils.parseDocument(xml)\n  encrypted \u003c- XmlSecurity.encryptDocument(\n                  certificate,\n                  \"http://www.w3.org/2001/04/xmlenc#aes256-cbc\",\n                  \"http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p\",\n                  \"http://www.w3.org/2001/04/xmlenc#sha256\"\n               )(dom)\n} yield encrypted\n```\n\nDecrypt XML document\n--------------------\nAPI:\n```scala\ndef decryptDocument(key: Key)(dom: Document): Try[Document]\n```\nExample:\n```scala\nimport scala.util.Try\nimport org.w3c.dom.Document\nimport java.security.KeyPair\nimport com.github.arturopala.xmlsecurity.{XmlUtils,XmlSecurity}\n\nval keyPair: KeyPair = ???\nval xml: String = ??? // some XML string\nval document: Try[Document] = for {\n  dom       \u003c- XmlUtils.parseDocument(xml)\n  decrypted \u003c- XmlSecurity.decryptDocument(keyPair.getPrivate)(dom)\n} yield decrypted\n```\n\nHandy implicit methods - DOM api extensions\n-----------------------------------------------\n\n```scala\nimport com.github.arturopala.xmlsecurity.XmlOps._\n```\n\n### org.w3c.dom.Document\n\n```scala\ndef selectNodes(query: String, ns: Option[NamespaceContext] = None): Seq[Node]\ndef getTagTextContent(tag: String): Option[String]\ndef getAttributeValue(tag: String, attribute: String): Option[String]\ndef copy: Document\n```\n\n### org.w3c.dom.NodeList\n\n```scala\ndef toSeq: Seq[Node]\n```\n\n### org.w3c.dom.Node\n\n```scala\ndef children: Seq[Node]\ndef attributes: collection.Map[String, String]\ndef toJson: org.json4s.JObject\n```\n\nDocument rendering\n------------------\nAPI:\n```scala\ndef printDocument(dom: Document): Try[String]\ndef prettyPrint(indent: Int)(dom: Document): Try[String]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturopala%2Fscala-xml-security","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farturopala%2Fscala-xml-security","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturopala%2Fscala-xml-security/lists"}