PHP code example of pacoorozco / openssh

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

    

pacoorozco / openssh example snippets


use PacoOrozco\OpenSSH\PrivateKey;
use PacoOrozco\OpenSSH\PublicKey;

// generating an OpenSSH key
$privateKey = PrivateKey::generate();
$publicKey = $privateKey->getPublicKey();

// (only RSA keys) keys can be used to encrypt/decrypt data
$data = 'my secret data';

$encryptedData = $publicKey->encrypt($data); // returns something unreadable
$decryptedData = $privateKey->decrypt($encryptedData); // returns 'my secret data'

use PacoOrozco\OpenSSH\PrivateKey;

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

use PacoOrozco\OpenSSH\PrivateKey;
use PacoOrozco\OpenSSH\PublicKey;

PrivateKey::fromFile($pathToPrivateKey);
PublicKey::fromFile($pathToPublicKey);

use PacoOrozco\OpenSSH\PrivateKey;
use PacoOrozco\OpenSSH\PublicKey;

PrivateKey::fromString($privateKeyContent);
PublicKey::fromString($publicKeyString);

use PacoOrozco\OpenSSH\PrivateKey;

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

use PacoOrozco\OpenSSH\PrivateKey;
use PacoOrozco\OpenSSH\PublicKey;

$data = 'my secret data';

$publicKey = PublicKey::fromFile($pathToPublicKey);
$encryptedData = $publicKey->encrypt($data); // encrypted data contains something unreadable

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

use PacoOrozco\OpenSSH\PrivateKey;

PrivateKey::fromFile($pathToPrivateKey)->canDecrypt($data); // returns a boolean;

use PacoOrozco\OpenSSH\PrivateKey;
use PacoOrozco\OpenSSH\PublicKey;

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

$publicKey = PublicKey::fromFile($pathToPublicKey);

$publicKey->verify('my message', $signature) // returns true;
$publicKey->verify('my modified message', $signature) // returns false;

use PacoOrozco\OpenSSH\Rules\PublicKeyRule;

[...]

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