From aa471d22b4b7e2906925efcd21f85e46bb8a7b75 Mon Sep 17 00:00:00 2001 From: giansalex Date: Thu, 25 Jan 2018 17:13:19 -0500 Subject: [PATCH] fix get hash from signed xml --- src/Report/XmlUtils.php | 34 +++-- tests/Report/XmlUtilsTest.php | 3 +- tests/Resources/dte_1513569521004.xml | 180 ++++++++++++++++++++++++++ 3 files changed, 206 insertions(+), 11 deletions(-) create mode 100644 tests/Resources/dte_1513569521004.xml diff --git a/src/Report/XmlUtils.php b/src/Report/XmlUtils.php index d88808a..7f7c8f9 100644 --- a/src/Report/XmlUtils.php +++ b/src/Report/XmlUtils.php @@ -24,7 +24,7 @@ final class XmlUtils public function getHashSign($xml) { $doc = new \DOMDocument(); - $doc->loadXML($xml); + @$doc->loadXML($xml); return $this->getHashSignFromDoc($doc); } @@ -37,7 +37,7 @@ public function getHashSign($xml) public function getHashSignFromFile($filename) { $doc = new \DOMDocument(); - $doc->load($filename); + @$doc->load($filename); return $this->getHashSignFromDoc($doc); } @@ -55,15 +55,8 @@ public function getHashSignFromDoc(\DOMDocument $document) if ($exts->length == 0) { return ''; } - $nodeSign = $exts->item($exts->length - 1); - $hash = $xpt->query('ext:ExtensionContent/ds:Signature/ds:SignedInfo/ds:Reference/ds:DigestValue', $nodeSign); - - if ($hash->length == 0) { - return ''; - } - - return $hash->item(0)->nodeValue; + return $this->getHash($exts, $xpt); } /** @@ -79,4 +72,25 @@ public function getXpath(\DOMDocument $document) return $xpt; } + + /** + * @param \DOMNodeList $exts + * @param \DOMXPath $xpt + * @return string + */ + public function getHash(\DOMNodeList $exts, \DOMXPath $xpt) + { + for ($i = $exts->length; $i-- > 0;) { + $nodeSign = $exts->item($i); + $hash = $xpt->query('ext:ExtensionContent/ds:Signature/ds:SignedInfo/ds:Reference/ds:DigestValue', $nodeSign); + + if ($hash->length == 0) { + continue; + } + + return $hash->item(0)->nodeValue; + } + + return ''; + } } diff --git a/tests/Report/XmlUtilsTest.php b/tests/Report/XmlUtilsTest.php index 45b5ced..6ccf8b7 100644 --- a/tests/Report/XmlUtilsTest.php +++ b/tests/Report/XmlUtilsTest.php @@ -33,7 +33,7 @@ protected function setUp() public function testExtractFromDoc($filename) { $doc = new \DOMDocument(); - $doc->load($filename); + @$doc->load($filename); $hash = $this->utils->getHashSignFromDoc($doc); $this->assertNotEmpty($hash); @@ -86,6 +86,7 @@ public function xmlPathProvider() return [ [$dir.'/20505310072-01-F001-00019772.xml'], [$dir.'/20505310072-03-B001-00000088.xml'], + [$dir.'/dte_1513569521004.xml'], ]; } } diff --git a/tests/Resources/dte_1513569521004.xml b/tests/Resources/dte_1513569521004.xml new file mode 100644 index 0000000..27f4b5b --- /dev/null +++ b/tests/Resources/dte_1513569521004.xml @@ -0,0 +1,180 @@ + + + + + +T8CQ0EjaW/LKbvjM38ShzEcWRrg=oRIMJb6Mop+FKbnzfMwTlxyoAjfCddHDlo4SsynI5s5DxUuFotg7YbeOwVvfvWWJHKkPIAj0ccna +BxlouBBdhnGajmPI+eIuLf8HU6svpgpUROtsRGhsBD3GDz5myb6T2Z5BogFnOIxdh05bxUCypNTy +oNvDWsuJoY6H5z4j8UwOLmwWQ/WaH5YLpJKNwSoutOasIMUcHpYYnExNfNQhE1mi3YokbWtlnEYe +CJpNxm7/lc2+YTe6BakI2/Q/VYXSdi1FLjyy3bekMn/qOqGihEg51TljDUjthn4JPeQg/3DtaYy2 +sFNp2ttasa+YfSpGoAJ/UZOQ+KahDFm6pWp9Ig==MIIFwzCCBKugAwIBAgIKbEu0bgAAAAAi6DANBgkqhkiG9w0BAQUFADCBjjELMAkGA1UEBhMCQ0gx +EDAOBgNVBAoTB1dJU2VLZXkxIjAgBgNVBAsTGUNvcHlyaWdodCAyMDExIFdJU2VLZXkgU0ExFjAU +BgNVBAsTDUludGVybmF0aW9uYWwxMTAvBgNVBAMTKFdJU2VLZXkgQ2VydGlmeUlEIEFkdmFuY2Vk +IFNlcnZpY2VzIENBIDIwHhcNMTUwNTI1MjIyMzE1WhcNMTgwNT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QQuMCwGCCsGAQUFBwMCBgorBgEEAYI3CgMMBggr +BgEFBQcDBAYKKwYBBAGCNxQCAjBDBgkrBgEEAYI3FQoENjA0MAoGCCsGAQUFBwMCMAwGCisGAQQB +gjcKAwwwCgYIKwYBBQUHAwQwDAYKKwYBBAGCNxQCAjBEBgkqhkiG9w0BCQ8ENzA1MA4GCCqGSIb3 +DQMCAgIAgDAOBggqhkiG9w0DBAICAIAwBwYFKw4DAgcwCgYIKoZIhvcNAwcwEQYJYIZIAYb4QgEB +BAQDAgCgMA0GCSqGSIb3DQEBBQUAA4IBAQBfZTujLumFCxTr+TXwa7NYsAB13U+lteC1zd92hJdE +cghjvxF058TyqGKtAqe/0mTGQYlZCXL448Dtr8Sj1N1KCbBUq+X1BTGl8Lj/2jlces7ZIfdiSvLP +LWRCi5fIz/O7J18YLY7ugA49Zq8bzWFkwjHULms0udRkuzwBFC6uVg4PhJAF7jWy01rkkiFgLTnu +FJ01Z6bk7MF+X6aIc2+kxaOa6qFveWpbaqw5DELrwfma66mk+wn5FKD47FK25A0oKBtscjk/ISUB +PPzQW+aaaZVGqBXORGb9JJT4M6QcuGt2FYQ1mHKLfgc6Jzbi1t5jchpssFd8Z9wAW5M3ASBh + + + + + +1001 +10.68 + + +2005 +0 + + +2001 +0 + + +1000 + + + + + + +2.0 +1.0 +B561-365900 +2017-10-06 +03 +PEN + +SB561-365900 + + +20305354563 + + + + + + + +#SB561-365900-171006201427 + + + + +20305354563 +6 + + + + + +150115 + + +LIMA +LIMA +LA VICTORIA + +PE + + + + + + + + +0 +0 + + + + + + +PE + + + + + + +1.92 + +1.92 + + +1000 +IGV +VAT + + + + + +10.68 +1.92 +12.6 + + +1 +1.0 +10.68 + + +12.6 +01 + + + +false +0.0 + + +1.92 + +10.68 +1.92 +18.0 + +VAT +10 +00 + +1000 +IGV +VAT + + + + + + + +189993 + + + +10.68 + + +