cabreran/xmlseclibs

PHP XML 安全库

v3.1.0 2020-07-17 19:46 UTC

This package is auto-updated.

Last update: 2024-09-18 05:33:30 UTC


README

#xmlseclibs

xmlseclibs 是一个用 PHP 编写的库,用于处理 XML 加密和签名。

要求

xmlseclibs 需要 PHP 版本 7.1 或更高。

如何安装

使用 composer.phar 安装。

php composer.phar require "cabreran/xmlseclibs"

使用案例

xmlseclibs 被许多不同的软件所使用。

基本用法

以下示例展示了 xmlseclibs 的基本用法,带有 SHA-256 签名。

use Cabreran\XMLSecLibs\XMLSecurityDSig;
use Cabreran\XMLSecLibs\XMLSecurityKey;

// Load the XML to be signed
$doc = new DOMDocument();
$doc->load('./path/to/file/tobesigned.xml');

// Create a new Security object 
$objDSig = new XMLSecurityDSig();
// Use the c14n exclusive canonicalization
$objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
// Sign using SHA-256
$objDSig->addReference(
    $doc, 
    XMLSecurityDSig::SHA256, 
    array('http://www.w3.org/2000/09/xmldsig#enveloped-signature')
);

// Create a new (private) Security key
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type'=>'private'));
/*
If key has a passphrase, set it using
$objKey->passphrase = '<passphrase>';
*/
// Load the private key
$objKey->loadKey('./path/to/privatekey.pem', TRUE);

// Sign the XML file
$objDSig->sign($objKey);

// Add the associated public key to the signature
$objDSig->add509Cert(file_get_contents('./path/to/file/mycert.pem'));

// Append the signature to the XML
$objDSig->appendSignature($doc->documentElement);
// Save the signed XML
$doc->save('./path/to/signed.xml');