Skip to content

Commit

Permalink
Byajh
Browse files Browse the repository at this point in the history
  • Loading branch information
strey223 committed Sep 29, 2021
1 parent f6110b7 commit ae613de
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions CryptoProCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ public static function verifyFileContent($fileContent)
}
}

/**
* Проверить, что содержимое файла подписано правильной подписью открепленной подписью
*
*
* @param $fileSignContent
* @param $fileToBeSigned
* @throws Cli
* @throws SignatureError
*/
public static function verifyFileContentDetached($fileSignContent, $fileToBeSignedContent)
{
$fileSign = tempnam(sys_get_temp_dir(), 'cpc');
$fileToBeSigned = tempnam(sys_get_temp_dir(), 'ftbs');
file_put_contents($fileSign, $fileSignContent);
file_put_contents($fileToBeSigned, $fileToBeSignedContent);
try {
self::verifyFileDetached($fileSign, $fileToBeSigned);
} finally {
unlink($fileSign);
unlink($fileToBeSigned);
}
}

private static function getDevNull()
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
Expand Down Expand Up @@ -142,4 +165,29 @@ public static function verifyFile($file)
throw new Cli("Неожиданный результат $shellCommand: \n$result");
}
}

/**
* Проверить, что файл подписан правильной открепленной подписью подписью
*
* @param $fileSign
* @param $fileToBeSigned
* @throws Cli
* @throws SignatureError
*/
public static function verifyFileDetached($fileSign, $fileToBeSigned)
{
//Пример cryptcp.exe -verify y:\text.txt -detached -nochain -f y:\signature.sig -dir y:\
$shellCommand = 'yes "n" 2> '.self::getDevNull() . ' | ' . escapeshellarg(self::$cryptcpExec) . ' -verify -verall ' . escapeshellarg($fileSign)
. ' -detached -f ' . escapeshellarg($fileToBeSigned);
$result = shell_exec($shellCommand);
if (strpos($result, "[ErrorCode: 0x00000000]") === false && strpos($result, "[ReturnCode: 0]") === false) {
preg_match('#\[ErrorCode: (.+)\]#', $result, $matches);
$code = strtolower($matches[1]);
if (isset(self::ERROR_CODE_MESSAGE[$code])) {
throw new SignatureError(self::ERROR_CODE_MESSAGE[$code]);
}
throw new Cli("Неожиданный результат $shellCommand: \n$result");
}
}

}

0 comments on commit ae613de

Please sign in to comment.