PHPackages                             pacoorozco/openssh - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. pacoorozco/openssh

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pacoorozco/openssh
==================

Creating and loading private/public OpenSSH keys

v0.5.2(2mo ago)024.9k↓69.6%2[1 PRs](https://codeberg.org/pakus/openssh/pulls)MITPHPPHP ^8.1CI passing

Since Jun 20Pushed 7mo ago1 watchersCompare

[ Source](https://codeberg.org/pakus/openssh)[ Packagist](https://packagist.org/packages/pacoorozco/openssh)[ Docs](https://codeberg.org/pakus/openssh)[ RSS](/packages/pacoorozco-openssh/feed)WikiDiscussions main Synced today

READMEChangelog (5)Dependencies (8)Versions (10)Used By (0)

Creating and loading private/public OpenSSH keys
================================================

[](#creating-and-loading-privatepublic-openssh-keys)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d7c0e7f2654edfc073a8ff97fb7979ce06d247d2501625b5a9fbaae62be0b5e6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7061636f6f726f7a636f2f6f70656e7373682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/crypto)[![Tests](https://github.com/pacoorozco/openssh/workflows/Tests/badge.svg)](https://github.com/pacoorozco/openssh/workflows/Tests/badge.svg)

This package allows you to easily generate OpenSSH private/public key pairs, which can be used as authentication method in SSH connections.

```
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'
```

Most functions in this package are wrappers around [phpseclib](https://phpseclib.com) functions.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require pacoorozco/openssh
```

Usage
-----

[](#usage)

You can generate a private key using the `generate` function and saving it to a file:

```
use PacoOrozco\OpenSSH\PrivateKey;

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

### Loading keys

[](#loading-keys)

To load a key from a file use the `fromFile` static method:

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

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

Alternatively, you can also create a key object using a string.

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

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

At any time, you can obtain the public key from a private key

```
use PacoOrozco\OpenSSH\PrivateKey;

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

### \[RSA keys only\] Encrypting a message with a public key, decrypting with the private key

[](#rsa-keys-only-encrypting-a-message-with-a-public-key-decrypting-with-the-private-key)

Here's how you can encrypt data using the public key, and how to decrypt it using the private key.

```
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'
```

If `decrypt` cannot decrypt the given data (maybe a non-matching public key was used to encrypt the data, or maybe tampered with the data), an exception of class `\PacoOrozco\OpenSSH\Exceptions\BadDecryptionException` will be thrown.

### Determining if the data can be decrypted

[](#determining-if-the-data-can-be-decrypted)

The `PrivateKey` class has a `canDecrypt` method to determine if given data can be decrypted.

```
use PacoOrozco\OpenSSH\PrivateKey;

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

### Signing and verifying data

[](#signing-and-verifying-data)

The `PrivateKey` class has a method `sign` to generate a signature for the given data. The `verify` method on the `PublicKey` class can be used to verify if a signature is valid for the given data.

If `verify` returns `true`, you know for certain that the holder of the private key signed the message, and that it was not tampered with.

```
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;
```

### Validating inputs (Laravel)

[](#validating-inputs-laravel)

You can use this library to validate form inputs.

To validate if an input is a valid public or private key you can use:

```
use PacoOrozco\OpenSSH\Rules\PublicKeyRule;

[...]

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

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance73

Regular maintenance activity

Popularity27

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~251 days

Recently: every ~352 days

Total

8

Last Release

80d ago

PHP version history (2 changes)v0.1.0PHP ^8.0

v0.5.1PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1770956?v=4)[Maciej Krzysztofinski](/maintainers/pakus)[@pakus](https://github.com/pakus)

---

Top Contributors

[![pacoorozco](https://avatars.githubusercontent.com/u/2443200?v=4)](https://github.com/pacoorozco "pacoorozco (46 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (2 commits)")

---

Tags

laravelcryptossh-keysopenssh

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pacoorozco-openssh/health.svg)

```
[![Health](https://phpackages.com/badges/pacoorozco-openssh/health.svg)](https://phpackages.com/packages/pacoorozco-openssh)
```

###  Alternatives

[laravel/socialite

Laravel wrapper around OAuth 1 &amp; OAuth 2 libraries.

5.7k108.5M886](/packages/laravel-socialite)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[vinkius-labs/laravel-page-speed

Laravel Page Speed

2.5k12.5k1](/packages/vinkius-labs-laravel-page-speed)[emargareten/inertia-modal

Inertia Modal is a Laravel package that lets you implement backend-driven modal dialogs for Inertia apps.

90142.9k](/packages/emargareten-inertia-modal)[wearepixel/laravel-cart

A cart implementation for Laravel

1374.8k](/packages/wearepixel-laravel-cart)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
