我已经对XML进行了签名,但我不知道如何在签名中包含KeyValue元素。拥有一些文档可以节省很多时间。
下面的代码(如果您感兴趣的话)是我到目前为止对xmlseclibs所做的工作:
<?php
require('xmlseclibs.php'); XML字符串
$getToken = '<getToken>
<item>
<Semilla>Random string</Semilla>
</item>
</getToken>';创建XML对象(要签名)
$getToken_DOMDocument = new DOMDocument();
$getToken_DOMDocument -> loadXml($getToken); 使用xmlseclib创建签名对象
$getToken_XMLSecurityDSig = new XMLSecurityDSig();
$getToken_XMLSecurityDSig -> setCanonicalMethod(XMLSecurityDSig::C14N); 试图关闭不起作用的ds:前缀
$options['prefix'] = '';
$options['prefix_ns'] = '';
$options['force_uri'] = TRUE;
$options['id_name'] = 'ID';
$getToken_XMLSecurityDSig -> addReference($getToken_DOMDocument, XMLSecurityDSig::SHA1, array('http://www.w3.org/2000/09/xmldsig#enveloped-signature', 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'), $options); 访问必要的密钥数据
$XMLSecurityKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type'=>'private'));
$XMLSecurityKey -> loadKey('../../DTE/certificado/firma/certificado.pem', TRUE);
/* if key has Passphrase, set it using $objKey -> passphrase = <passphrase> */ 对XML对象签名
$getToken_XMLSecurityDSig -> sign($XMLSecurityKey); 添加公钥
$getToken_XMLSecurityDSig -> add509Cert(file_get_contents('../../DTE/certificado/firma/certificado.pem')); 将信封签名附加到XML对象
$getToken_XMLSecurityDSig -> appendSignature($getToken_DOMDocument -> documentElement); 将签名的XML代码保存到文件中。
$getToken_DOMDocument -> save('sign-basic-test.xml');
?>此外,还想从这个图书馆:
我从在这里输入链接描述得到了图书馆
提前谢谢。
发布于 2014-07-23 23:15:05
下面列出了一些可能有助于解决这些问题的链接:
http://code.google.com/p/xmlseclibs/issues/attachmentText?id=6&aid=-1080340148797098435&name=example.php&token=81f737657f6cf89b3e7fcdb6cc15033b
http://code.google.com/p/xmlseclibs/issues/detail?id=13
不确定它能解决所有问题,但应该对你有所帮助。
发布于 2015-02-21 10:02:40
我编写了一个名为xmldsig的facade库,以简化下划线XMLSecLibs的使用
在这个库中,代码的结果如下:
public function testSign()
{
$getToken = '<getToken>
<item>
<Semilla>Random string</Semilla>
</item>
</getToken>';
$data = new DOMDocument();
$data->loadXml($getToken);
$adapter = new XmlseclibsAdapter();
$adapter
->setPrivateKey(file_get_contents('privateKey.pem'))
->setPublicKey(file_get_contents('publicKey.pem'))
->setCanonicalMethod('http://www.w3.org/2001/10/xml-exc-c14n#')
->sign($data);
echo $data->saveXML();
);
}https://stackoverflow.com/questions/24922384
复制相似问题