PHPackages                             schno01/xmldsig - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. schno01/xmldsig

ActiveLibrary[Security](/categories/security)

schno01/xmldsig
===============

Sign XML Documents with Digital Signatures

0661PHP

Since Jun 24Pushed 10mo agoCompare

[ Source](https://github.com/schno01/xmldsig)[ Packagist](https://packagist.org/packages/schno01/xmldsig)[ RSS](/packages/schno01-xmldsig/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (1)

XMLDSIG for PHP
===============

[](#xmldsig-for-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/63587ec95a33c2112c0d60aec4d6b2273a1bc2676b50a534eea3b3ac24bc0709/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73656c6563746976652d7068702f786d6c647369672e737667)](https://packagist.org/packages/selective/xmldsig)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build Status](https://github.com/selective-php/xmldsig/workflows/build/badge.svg)](https://github.com/selective-php/xmldsig/actions)[![Coverage Status](https://camo.githubusercontent.com/4bb128dd3bdee07cca7a2736dc11d59b1e1f4dc051f4ae86dfe37d5c95d23a61/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f73656c6563746976652d7068702f786d6c647369672e737667)](https://scrutinizer-ci.com/g/selective-php/xmldsig/code-structure)[![Quality Score](https://camo.githubusercontent.com/1e835d042f45065f9c6e9dcf3346a9fc0b0a0269a1509a6378a9278b4bcbbdf5/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f73656c6563746976652d7068702f786d6c647369672e737667)](https://scrutinizer-ci.com/g/selective-php/xmldsig/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/8bb62cbc516a6169b5e20da4ce82078eb496ff53bb3cabc474a1a22f4cf09c93/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73656c6563746976652f786d6c647369672e737667)](https://packagist.org/packages/selective/xmldsig/stats)

Features
--------

[](#features)

- Sign XML Documents with Digital Signatures ([XMLDSIG](https://www.w3.org/TR/xmldsig-core/))
- Verify the Digital Signatures of XML Documents
- ECDSA (SHA256) signature
- Added Support for

Requirements
------------

[](#requirements)

- PHP 8.1+
- The openssl extension
- A X.509 digital certificate

Installation
------------

[](#installation)

```
composer require schno01/xmldsig

```

Usage
-----

[](#usage)

### Signing an XML Document with a digital signature

[](#signing-an-xml-document-with-a-digital-signature)

Input file: example.xml

```

        19834209
        02/02/2025

```

Load and add the private key to the `PrivateKeyStore`:

```
use Selective\XmlDSig\PrivateKeyStore;
// ...

$privateKeyStore = new PrivateKeyStore();

// load a private key from a string
$privateKeyStore->loadFromPem('private key content', 'password');

// or load a private key from a PEM file
$privateKeyStore->loadFromPem(file_get_contents('filename.pem'), 'password');

// load pfx PKCS#12 certificate from a string
$privateKeyStore->loadFromPkcs12('pfx content', 'password');

// or load PKCS#12 certificate from a file
$privateKeyStore->loadFromPkcs12(file_get_contents('filename.p12'), 'password');
```

Define the digest method: sha1, sha224, sha256, sha384, sha512

```
use Selective\XmlDSig\Algorithm;

$algorithm = new Algorithm(Algorithm::METHOD_SHA1);
```

Create a `CryptoSigner` instance:

```
use Selective\XmlDSig\CryptoSigner;

$cryptoSigner = new CryptoSigner($privateKeyStore, $algorithm);
```

Signing:

```
use Selective\XmlDSig\XmlSigner;

// Create a XmlSigner and pass the crypto signer
$xmlSigner = new XmlSigner($cryptoSigner);

// Optional: Set reference URI
$xmlSigner->setReferenceUri('');

// Create a signed XML string
$signedXml = $xmlSigner->signXml('
