pacoorozco/openssh 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pacoorozco/openssh

Composer 安装命令:

composer require pacoorozco/openssh

包简介

Creating and loading private/public OpenSSH keys

README 文档

README

Latest Version on Packagist

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 functions.

Installation

You can install the package via composer:

composer require pacoorozco/openssh

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

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

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

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

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)

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

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

pacoorozco/openssh 适用场景与选型建议

pacoorozco/openssh 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.03k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「crypto」 「laravel」 「ssh-keys」 「openssh」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pacoorozco/openssh 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pacoorozco/openssh 我们能提供哪些服务?
定制开发 / 二次开发

基于 pacoorozco/openssh 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 25.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-20