selective/xmldsig
最新稳定版本:3.2.0
Composer 安装命令:
composer require selective/xmldsig
包简介
Sign XML Documents with Digital Signatures
关键字:
README 文档
README
Features
- Sign XML Documents with Digital Signatures (XMLDSIG)
- Verify the Digital Signatures of XML Documents
- ECDSA (SHA256) signature
Requirements
- PHP 8.1 - 8.5
- The openssl extension
- A X.509 digital certificate
Installation
composer require selective/xmldsig
Usage
Signing an XML Document with a digital signature
Input file: example.xml
<?xml version="1.0"?> <root> <creditcard> <number>19834209</number> <expiry>02/02/2025</expiry> </creditcard> </root>
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('<?xml ...'); // or sign an XML file $signedXml = $xmlSigner->signXml(file_get_contents($filename)); // or sign an DOMDocument $xml = new DOMDocument(); $xml->preserveWhiteSpace = true; $xml->formatOutput = false; $xml->loadXML($data); $signedXml = $xmlSigner->signDocument($xml);
Output:
<?xml version="1.0"?> <root> <creditcard> <number>19834209</number> <expiry>02/02/2025</expiry> </creditcard> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> <SignedInfo> <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/> <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"/> <Reference URI=""> <Transforms> <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> </Transforms> <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha512"/> <DigestValue>Base64EncodedValue==</DigestValue> </Reference> </SignedInfo> <SignatureValue>AnotherBase64EncodedValue===</SignatureValue> </Signature> </root>
Signing only specific part of an XML document
Example:
use Selective\XmlDSig\Algorithm; use Selective\XmlDSig\CryptoSigner; use Selective\XmlDSig\PrivateKeyStore; use Selective\XmlDSig\XmlSigner; use DOMDocument; use DOMXPath; // ... // Load the XML content you want to sign $xml = new DOMDocument(); $xml->preserveWhiteSpace = true; $xml->formatOutput = false; $xml->loadXML($data); // Create a XPATH query to select the element you want to sign $xpath = new DOMXPath($xml); // Change this query according to your requirements $referenceUri = '#1'; $elementToSign = $xpath->query( '//*[@Id="'. $referenceUri .'"]' )->item(0); // Add private key $privateKeyStore = new PrivateKeyStore(); $privateKeyStore->loadPrivateKey('private key content', 'password'); $cryptoSigner = new CryptoSigner($privateKeyStore, new Algorithm(Algorithm::METHOD_SHA1)); // Sign the element $xmlSigner = new XmlSigner($cryptoSigner); $signedXml = $xmlSigner->signDocument($xml, $elementToSign);
Signing an XML Document with ECDSA SHA256
The Elliptic Curve Digital Signature Algorithm (ECDSA) is the elliptic curve analogue of the Digital Signature Algorithm (DSA).
It is compatible with OpenSSL and uses elegant math such as Jacobian Coordinates to speed up the ECDSA on pure PHP.
Requirements
- The GMP extension must be installed and enabled.
To install the package with Composer, run:
composer require starkbank/ecdsa
Example
Note, you can sign an XML signature using ECDSA. It's not supported to use ECDSA for the digest.
You can find a fully working example in the XmlEcdsaTest test class.
Verify the Digital Signatures of XML Documents
Load the public key(s):
use Selective\XmlDSig\PublicKeyStore; use Selective\XmlDSig\CryptoVerifier; use Selective\XmlDSig\XmlSignatureVerifier; $publicKeyStore = new PublicKeyStore(); // load a public key from a string $publicKeyStore->loadFromPem('public key content'); // or load a public key file $publicKeyStore->loadFromPem(file_get_contents('cacert.pem')); // or load a public key from a PKCS#12 certificate string $publicKeyStore->loadFromPkcs12('public key content', 'password'); // or load a public key from a PKCS#12 certificate file $publicKeyStore->loadFromPkcs12(file_get_contents('filename.pfx'), 'password'); // Load public keys from DOMDocument X509Certificate nodes $publicKeyStore->loadFromDocument($xml); // Load public key from existing OpenSSLCertificate resource $publicKeyStore->loadFromCertificate($certificate);
Create a CryptoVerifier instance:
use Selective\XmlDSig\CryptoVerifier; $cryptoVerifier = new CryptoVerifier($publicKeyStore);
Verifying:
use Selective\XmlDSig\XmlSignatureVerifier; // Create a verifier instance and pass the crypto decoder $xmlSignatureVerifier = new XmlSignatureVerifier($cryptoVerifier); // Verify XML from a string $isValid = $xmlSignatureVerifier->verifyXml($signedXml); // or verify a XML file $isValid = $xmlSignatureVerifier->verifyXml(file_get_contents('signed.xml')); // or verifying an DOMDocument instance $xml = new DOMDocument(); $xml->preserveWhiteSpace = true; $xml->formatOutput = false; $xml->loadXML($data); $isValid = $xmlSignatureVerifier->verifyDocument($xml); if ($isValid === true) { echo 'The XML signature is valid.'; } else { echo 'The XML signature is not valid.'; }
Online XML Digital Signature Verifier
Try these excellent online tools to verify XML signatures:
Similar libraries
License
The MIT License (MIT). Please see License File for more information.
selective/xmldsig 适用场景与选型建议
selective/xmldsig 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 441.79k 次下载、GitHub Stars 达 74, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「xmldsig」 「verify」 「signatures」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 selective/xmldsig 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 selective/xmldsig 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 selective/xmldsig 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a Facade to help validate and verify email addresses
Load DOM document safety
A PHP library for XML Security
Added a method to Laravel response for handling xml response and also converting Eloquent return to XML.
A PHP library for XML Security
Uses reflection to map XML to PHP objects.
统计信息
- 总下载量: 441.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 74
- 点击次数: 32
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04