Created
April 15, 2021 22:26
-
-
Save kernusr/aded2c230a05776bdbd456320f6ae33f to your computer and use it in GitHub Desktop.
Пример чтения подписи из файла
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"sop/asn1": "^4.1", | |
"webmasterskaya/x509": "dev-master" | |
}, | |
"minimum-stability": "dev" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Sop\ASN1\Element; | |
use Sop\ASN1\Type\Constructed\Sequence; | |
use Webmasterskaya\X509\Certificate\Certificate; | |
require_once __DIR__ . './vendor/autoload.php'; | |
$content = file_get_contents(__DIR__ . '/Polozhenie.pdf'); | |
$regexp | |
= '#ByteRange\[\s*(\d+) (\d+) (\d+)#'; // subexpressions are used to extract b and c | |
$result = []; | |
preg_match_all($regexp, $content, $result); | |
if (isset($result[2]) && isset($result[3]) && isset($result[2][0]) | |
&& isset($result[3][0]) | |
) { | |
$start = $result[2][0]; | |
$end = $result[3][0]; | |
if ($stream = fopen(__DIR__ . '/Polozhenie.pdf', 'rb')) { | |
$signature = stream_get_contents( | |
$stream, $end - $start - 2, $start + 1 | |
); // because we need to exclude < and > from start and end | |
fclose($stream); | |
} | |
if (!empty($signature)) { | |
$binary = hex2bin($signature); | |
$seq = Sequence::fromDER($binary); | |
$signed_data = $seq->getTagged(0)->asExplicit()->asSequence(); | |
$ecac = $signed_data->getTagged(0)->asImplicit(Element::TYPE_SET) | |
->asSet(); | |
/** @var Sop\ASN1\Type\UnspecifiedType $ecoc */ | |
$ecoc = $ecac->at($ecac->count() - 1); | |
$cert = Certificate::fromASN1($ecoc->asSequence()); | |
foreach ($cert->tbsCertificate()->subject()->all() as $attr) { | |
/** @var Webmasterskaya\X501\ASN1\AttributeTypeAndValue $atv */ | |
$atv = $attr->getIterator()->current(); | |
echo $atv->type()->typeName() . ' : ' . $atv->value()->stringValue() . PHP_EOL; | |
} | |
} | |
} |
Класс, потребовалось включить расширение в php "bcmath", но это работает, то что нужно, имеем все необходимые поля.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Теперь понял!
Они различаются чисто представлением. То что показывает криптопро - 16-битной представление, а то что возвращает класс - простой int
Можно конвертировать!
И потом просто сделать
Вернёт
5E110D84305C855577E75FBFC50F92B71AF6ACF6