Recommend this page to a friend!
  Classes of Kjell-Inge Gustafsson  >  PHP XML Signature  >  >  Download  
Role: Documentation
Content type: text/markdown
Description: Read me
Class: PHP XML Signature
Parse and create XML documents signed digitally
Author: By
Last change:
Date: 1 year ago
Size: 6,616 bytes


Class file image Download


  • PHP SDK of XML Digital Signature recomendation
  • based on the [XSD] schema

and provide

For help finding a good PHP cryptography library, please review * Choosing the Right Cryptography Library for your PHP Project: A Guide

Usage, parse XML

To parse an Dsig (Signature root) XML file (using XMLReader) :

namespace Kigkonsult\DsigSdk;
use Kigkonsult\DsigSdk\XMLParse\DsigParser;

$dsig = DsigParser::factory()->parse( 
    file_get_contents( 'DsigFile.xml' )

$signedInfo = $dsig->getsignedInfo();

The XML parser save the XMLreader node properties (baseURI, localName, name, namespaceURI, prefix) for each XML (Dto) element as 'XMLattributes' as well as XML attributes (xmlns, xmlns:*, schemaLocation), if set (more info below).

'any' [XSD] elements are accepted as 'Anytype' object instances (more info below, 'AnyType').

Usage, build up structure

To build up dsig structure:

namespace Kigkonsult\DsigSdk;
use Kigkonsult\DsigSdk\Dto\AnyType;
use Kigkonsult\DsigSdk\Dto\CanonicalizationMethodType;
use Kigkonsult\DsigSdk\Dto\KeyInfoType;
use Kigkonsult\DsigSdk\Dto\SignedInfoType;
use Kigkonsult\DsigSdk\Dto\SignatureType;
use Kigkonsult\DsigSdk\Dto\SignatureValueType;

$dsig = SignatureType::factory()
                    ->setAlgorithm( SignatureType::MINICANONICAL )
                    ->setAny( [
                            ->setElementName( 'nonSchemaElement1')
                            ->setAttributes( [
                                'id' => '12345' 
                                ] )
                             ->setContent( 'Lr1mKGxP7VAgMB...' ),
                            ->setElementName( 'nonSchemaElement2')
                            ->setSubElements( [
                                    ->setElementName( 'nonSchemaElement3')
                                    ->setContent( 'Lr1mKGxP7VAgMB...' ),
                            ] )
            ->setSignatureValueType( 'vgGZnRlm8...' )
            ->setKeyInfoType( [
                [                 // one set of elements
                    [             // element
                        SignatureType::X509DATA => 
                                ->setX509Certificate( ... )
        ] )
XML attributes

You can set (single 'element') XMLattribute using

$dsig->setXMLAttribut( <key>, <value> );

To set (ex. prefix) and 'propagate' down in hierarchy:

$dsig->setXMLAttribut( SignatureType::PREFIX, <value>, true );

You can remove (single 'element') XMLattribute using

$dsig->unsetXMLAttribut( <key> );

To unset (ex. prefix) and 'propagate' down in hierarchy:

$dsig->unsetXMLAttribut( SignatureType::PREFIX, true );

To fetch and iterate over XMLAttributes

foreach( $dsig->getXMLAttributes() as $key => $value {


Anytype object instances are used for 'any' [XSD] elements. The element name are stored and fetched with

$anytype->setElementName( <name> );
$anytypeName = $anytype->getElementName();

The 'any' [XSD] element attributes may include XML attributes.

The AnyType attributes are stored and fetched as array.

$anytype->setAttributes( [ <key> => <value> ] );
foreach( $anytype->getAttributes() as $key => $value {

Note, an AnyType instance may have * content * type string, * AnyType::setContent() * AnyType::getContent()

or * sub-elements type array [AnyType] * AnyType::setSubElements() * AnyType::getSubElements()

but not both.

Usage, output as XML

DsigSdk uses XMLWriter creating output.

$XMLstring = DsigWriter::factory()->write( $dsig );

The XMLwriter adds for each element * element name with prefix, if exists XMLattribute xmlns, xmlns: and schemaLocation, if exists.

Usage, output as DomNode

$domNode = DsigWriter::factory()->write( $dsig, true );


For class structure and architecture, please review * the [XSD] * docs/Dsig.png class design * the src/DsigLoader directory

You may find convenient constants in - src/DsigInterface - src/XMLAttributesInterface

For base64Encode/base64Decode/hash support, please review src/Impl/



From the Command Line:

composer require kigkonsult/dsigsdk

In your composer.json:

    "require": {
        "kigkonsult/dsigsdk": "dev-master"

Acquire access

namespace Kigkonsult\DsigSdk;
include 'vendor/autoload.php';

Run tests

cd pathToSource/DsigSdk


Download and acquire..

namepace Kigkonsult\DsigSdk;
include 'pathToSource/DsigSdk/autoload.php';


For support, please use [Github]/issues.


This project is licensed under the LGPLv3 License

[Composer]: [Github]: []: [XSD]:

For more information send a message to info at phpclasses dot org.