mohitjangra/xmlseclibs

用于XML安全的PHP库

3.1.6 2022-05-19 08:44 UTC

README

#xmlseclibs

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

xmlseclibs的作者是Rob Richards。

分支

目前只有master分支是活跃维护的。

  • master/3.1: 添加了对AES-GCM的支持,需要7.1+版本
  • 3.0: 移除了对mcrypt的使用,需要5.4+版本(出于安全原因推荐使用5.6.24+版本)
  • 2.0: 包含对命名空间的支持,需要5.3+版本
  • 1.4: 包含自动加载支持,同时保持对较旧的1.3版本的向后兼容性,使用xmlseclibs.php文件。支持PHP 5.2+版本

要求

xmlseclibs需要PHP版本5.4或更高版本。 出于安全原因推荐使用5.6.24+版本

如何安装

使用composer.phar进行安装。

php composer.phar require "mohitjangra/xmlseclibs"

使用案例

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

基本用法

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

use RobRichards\XMLSecLibs\XMLSecurityDSig;
use RobRichards\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');

如何贡献

邮件列表: https://groups.google.com/forum/#!forum/xmlseclibs