PHP code example of swissmakers / php-openssh

1. Go to this page and download the library: Download swissmakers/php-openssh 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/ */

    

swissmakers / php-openssh example snippets


use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

// Generate a new OpenSSH key pair
$privateKey = PrivateKey::generate();
$publicKey = $privateKey->getPublicKey();

// Encrypt and decrypt data using the key pair
$data = 'my secret data';
$encryptedData = $publicKey->encrypt($data); // Returns unreadable data
$decryptedData = $privateKey->decrypt($encryptedData); // Returns 'my secret data' in plaintext

use Swissmakers\OpenSSH\PrivateKey;

$privateKey = PrivateKey::generate();
$privateKey->toFile('/home/foo/bar');

use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$publicKey = PublicKey::fromFile($pathToPublicKey);

use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

$privateKey = PrivateKey::fromString($privateKeyContent);
$publicKey = PublicKey::fromString($publicKeyContent);

use Swissmakers\OpenSSH\PrivateKey;

$privateKey = PrivateKey::fromString($privateKeyContent);
$publicKey = $privateKey->getPublicKey();

use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

$data = 'my secret data';

$publicKey = PublicKey::fromFile($pathToPublicKey);
$encryptedData = $publicKey->encrypt($data);

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$decryptedData = $privateKey->decrypt($encryptedData); // Returns 'my secret data'

use Swissmakers\OpenSSH\PrivateKey;

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$canDecrypt = $privateKey->canDecrypt($data); // Returns a boolean

use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$signature = $privateKey->sign('my message'); // Returns a string

$publicKey = PublicKey::fromFile($pathToPublicKey);
$isVerified = $publicKey->verify('my message', $signature); // Returns true
$isModifiedVerified = $publicKey->verify('my modified message', $signature); // Returns false

use Swissmakers\OpenSSH\Rules\PublicKeyRule;
use Swissmakers\OpenSSH\Rules\PrivateKeyRule;

public function rules(): array
{
    return [
        'public_key' => [
            new PublicKeyRule(),
        ],
        'private_key' => [
            new PrivateKeyRule(),
        ],
    ];
}
bash
composer