PHPackages                             selective/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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. selective/xmldsig

Abandoned → [robrichards/xmlseclibs](/?search=robrichards%2Fxmlseclibs)Library[Parsing &amp; Serialization](/categories/parsing)

selective/xmldsig
=================

Sign XML Documents with Digital Signatures

3.2.0(6mo ago)74487.0k—5.3%39[1 issues](https://github.com/selective-php/xmldsig/issues)[1 PRs](https://github.com/selective-php/xmldsig/pulls)3MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0CI passing

Since Aug 1Pushed 6mo ago2 watchersCompare

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

READMEChangelog (10)Dependencies (5)Versions (18)Used By (3)

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)[![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

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

[](#requirements)

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

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

[](#installation)

```
composer require selective/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('
