PHP code example of mikhailovlab / crypto-pro-builder

1. Go to this page and download the library: Download mikhailovlab/crypto-pro-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mikhailovlab / crypto-pro-builder example snippets

 
protected ConsoleRunner $csp;
 
protected array $patterns;
 
protected ?array $pattern;
 
protected ?array $expectedFields;
 
protected bool $structureMatches;
 
protected array $methods;
 
public function __construct(string $command = 'csptest')
 
public function __call(string $name, array $arguments): self

public function registerMethods(array $customMethods): self

public function addKey(string $key): self

public function getContainers(bool $fullPath = true): self

public function checkContainer(?string $container = null, ?string $pass = null): self

public function changeContainerPass(?string $container = null, ?string $newPass = null, ?string $currentPass = null): self

public function copyContainer(): self

public function deleteContainer(?string $container = null): self

public function hashFile(string $filePath, string $alg = 'GR3411_2012_512'): self

public function verifyHash(array $files, string $alg = 'GR3411_2012_512'): self

public function signDocument(): self

public function verifySignature(array $files): self

public function encryptDocument(): self

public function decryptDocument(): self

public function certificatInstall(): self

public function getCertificates(): self

public function getCertificateByTp(): self

public function deleteCertificate(): self

public function encoding(string $code): self

public function decoding(): self

public function usePattern(?string $patternName = null, ?array $patternFields = null): self

public function useStructure(): self

public function addPatterns(array $patterns): self

private function structureMatches(array $matches, array $fields): array

public function printBuilderString(): ?string

public function run(): array

private function success(): array

private function throwError(): never

try{
    dd(new CryptoPro()
        ->getContainers()
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:6 [▼ // app\Http\Controllers\TestController.php:23
  0 => "\\.\FAT12_H\d58fe6c13-d917-2a53-8e9c-8c4b8158220"
  1 => "\\.\FAT12_H\c3965c1c-56de-4ed2-a6bd-fcfe1f47f77f"
  2 => "\\.\FAT12_H\469233b1-9ccd-4a74-9264-a9b4837ad3b5"
  3 => "\\.\FAT12_H\e00b1f31-ecb7-4827-9c5b-f1460c682261"
  4 => "\\.\FAT12_H\015b6fa9-e71d-4240-be9f-0462b40e0042"
  5 => "\\.\FAT12_H\8ac2691d-c4d5-457e-8d47-3a52e5a2691a"
]

try{
    dd(new CryptoPro()
        ->checkContainer($container)
        //->password($password) пароль, если требуется
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:23
  "status" => "успешно"
]

try{
    dd(new CryptoPro()
        ->changeContainerPass($container, currentPass: '', newPass: '1234')
	->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:25
  "status" => "успешно"
]

try{
    dd(new CryptoPro()
        ->copyContainer()
        ->contsrc($container1) //входной контейнер
        ->contdest($container2) //выходной контейнер
        ->pinsrc('1234') //пароль входного контейнера, если требуется
        //->pindest() пароль выходного контейнера, если требуется
        ->silent() //не выводить окно с вводом пароля
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:25
  "status" => "успешно"
]

try{
    dd(new CryptoPro()
        ->deleteContainer($container)
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:25
  "status" => "успешно"
]

try{
    dd(new CryptoPro('cpverify') //либо полный путь
        ->hashFile('H:\csp\123.txt')
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:25
  "hash" => "275BF35756C49E7A35810893777AC4F5E0E56D3D24A259502C56F2CFA5048014A7496908CDB177C3B939E5D38CC51299E5D364226C0B8BEB80030CE86F6A1762"
]

try{
    dd(new CryptoPro('cpverify')
        ->verifyHash(['H:\csp\123.txt', $hash]) //второй элемент хэш строка или путь к файлу
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:25
  "status" => "File 'H:\csp\123.txt' has been verified"
]

try{
    dd(new CryptoPro()
        ->signDocument()
        ->in('H:\csp\123.txt')
        ->out('H:\csp\123.txt.sig')
        ->password('1234') //пароль, если требуется 
        ->my('00dad6c045c2ec4a01f20441daf2d8dd999aaf07') // Сертификат 
        ->addsigtime() //добавить время подписи, если требуется
        ->base64() // base64, если требуется
        ->detached() //отсоединенная подпись, если требуется
        ->add() //добавить сертификат, если требуется
        ->silent() //не выводить окно с вводом пароля
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:25
  "status" => "успешно"
]

$array = [
    'H:\csp\123.txt',
    'H:\csp\123.txt.sig' //для присоединенной подписи указываем только 1 файл
];

try{
    dd(new CryptoPro("cryptcp")
        ->encoding('866') // windows консоль выдаст кириллицу в 866
        ->verifySignature($array)
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:31
  0 => "RU, Москва, Сидоров Иван Иванович, [email protected]"
]

try{
    dd(new CryptoPro("cryptcp")
        ->encryptDocument()
        ->thumbprint('00dad6c045c2ec4a01f20441daf2d8dd999aaf07')
        ->addKey('H:\csp\123.txt')
        ->addKey('H:\csp\123.txt.enc')
        ->silent()
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:26
  "status" => "успешно"
]

try{
    dd(new CryptoPro("cryptcp")
        ->decryptDocument()
        ->thumbprint('00dad6c045c2ec4a01f20441daf2d8dd999aaf07')
        ->pin(1234)
        ->addKey('H:\csp\123.txt.enc')
        ->addKey('H:\csp\123decr.txt')
        ->silent()
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:26
  "status" => "успешно"
]

try{
    dd(new CryptoPro("certmgr.exe") //не путать со встроенной windows утилитой 
        ->certificatInstall()
        ->file('H:\csp\Cert.cer') //установить из файла
        //->cont($container) //установить из контейнера
        ->store('uMy')
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:26
  "status" => "успешно"
]

try{
    dd(new CryptoPro("certmgr.exe") //не путать со встроенной windows утилитой 
        ->getCertificates()
        ->encoding('866') // Windows консоль выдаст кириллицу в 866 
        ->store('uMy')
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:12 [▼ // app\Http\Controllers\TestController.php:26
  0 => array:5 [▼
   "subject" => "Сидоров Иван Иванович"
   "serialNumber" => "0x7C001F6C9ED2E51F47F4CB4020000D001F6C9E"
   "sha1" => "00dad6c045c2ec4a01f20441daf2d8dd999aaf07"
   "issued" => "30/05/2025  01:19:45 UTC"
   "expires" => "04/07/2025  10:36:28 UTC"
  ]
  1 => array:5 [▶]
  2 => array:5 [▶]
  3 => array:5 [▶]
  4 => array:5 [▶]
  5 => array:5 [▶]
  6 => array:5 [▶]
  7 => array:5 [▶]
  8 => array:5 [▶]
  9 => array:5 [▶]
  10 => array:5 [▶]
  11 => array:5 [▶]
]

try{
    dd(new CryptoPro("certmgr.exe") //не путать со встроенной windows утилитой 
        ->getCertificateByTp()
        ->encoding('866')
        ->store('uMy')
        ->thumbprint('00dad6c045c2ec4a01f20441daf2d8dd999aaf07')
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:5 [▼ // app\Http\Controllers\TestController.php:26
  "subject" => "Сидоров Иван Иванович"
  "serialNumber" => "0x7C001F6C9ED2E51F47F4CB4020000D001F6C9E"
  "sha1" => "00dad6c045c2ec4a01f20441daf2d8dd999aaf07"
  "issued" => "30/05/2025  01:19:45 UTC"
  "expires" => "04/07/2025  10:36:28 UTC"
]

try{
    dd(new CryptoPro("certmgr.exe") //не путать со встроенной windows утилитой 
        ->deleteCertificate()
        ->store('uMy')
        ->thumbprint('00dad6c045c2ec4a01f20441daf2d8dd999aaf07')
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}

array:1 [▼ // app\Http\Controllers\TestController.php:26
  "status" => "успешно"
]

try{
    dd(new CryptoPro()
        ->registerMethods(['install', 'delete']) //зарегистрировать дополнительные методы
        ->addKey("-nochain") //пробросить кастомный аргумент или ключ
        ->encoding('866') //добавить кодировку, например из 866 в UTF-8 (для кириллицы в windows)
        ->decoding() //вернуть исходную кодировку, если отдаем вывод в консоль 
        ->addPatterns(['patternname' => 'regexp']) //добавить кастомные паттерны для парсинга 
        ->usePattern("patternname") //использовать паттерн для парсинга 
        ->run()
    );
    
}catch (Exception $e){
    dd($e->getMessage());
}