承接 codeaken/sshkey 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

codeaken/sshkey

最新稳定版本:1.2

Composer 安装命令:

composer require codeaken/sshkey

包简介

Library for working with and generating SSH keys

README 文档

README

This library allows you to work with public and private keys in PHP.

  • Read keys both from a file and from other sources like a database
  • Convert between key formats
  • Get a public keys fingerprint
  • Generate new key-pairs

It depends on phpseclib for all of the key manipulation.

Installation

Require the package in your composer.json and then run composer update

"require": { ... "codeaken/sshkey": "1.*" ... }, 

Usage

These examples makes the following assumptions:

  • The library is autoloaded, for example by having included vendor/autoload.php somewhere in your project
  • You have a a key-pair in your current directory called id_rsa (private) and id_rsa.pub (public). The private key is not encrypted.
  • An encrypted private key in your current directory called id_encrypted_rsa protected by the password abc123

Read a key from a file

<?php // Read in the public and private keys $publicKey = SshPublicKey::fromFile('id_rsa.pub'); $privateKey = SshPrivateKey::fromFile('id_rsa'); $encryptedPrivateKey = SshPrivateKey::fromFile('id_encrypted_rsa', 'abc123'); // Try to read a key that does not exists; will throw a FileNotFoundException try { $missingKey = SshPublicKey::fromFile('nosuchkey.pub'); } catch (Codeaken\SshKey\Exception\FileNotFoundException $e) { echo 'Could not find the key'; } // Try to read an encrypted private key using the wrong password; will throw a // LoadKeyException try { $encryptedKey = SshPrivateKey::fromFile('id_encrypted_rsa', 'wrongpass'); } catch (Codeaken\SshKey\Exception\LoadKeyException $e) { echo 'Could not decrypt the private key'; }

Read a key from a non file source

<?php // In this case we will read the key data from a file for simplicity but it // could come from a database or some other source $publicKeyData = file_get_contents('id_rsa.pub'); $publicKey = new SshPublicKey($publicKeyData); $encryptedPrivateKeyData = file_get_contents('id_encrypted_rsa'); $privateKey = new SshPrivateKey($encryptedPrivateKeyData, 'abc123');

Get a public keys fingerprint and comment

<?php $publicKey = SshPublicKey::fromFile('id_rsa.pub'); echo $publicKey->getFingerprint(); echo $publicKey->getComment();

Generate a new keypair

<?php // 1024 bits and no passphrase $keyPair1 = SshKeyPair::generate(1024); // 2048 bits and a passphrase of abc123 $keyPair2 = SshKeyPair::generate(2048, 'abc123'); echo $keyPair2->getPrivateKey()->getKeyData(SshKey::FORMAT_PKCS8); echo $keyPair2->getPublicKey()->getKeyData(SshKey::FORMAT_OPENSSH);

Save a key to a file

<?php $keyPair = SshKeyPair::generate(); $publicKey = $keyPair->getPublicKey(); $privateKey = $keyPair->getPrivateKey(); file_put_contents('id_new_rsa.pub', $publicKey->getKeyData(SshKey::FORMAT_OPENSSH)); file_put_contents('id_new_rsa', $privateKey->getKeyData(SshKey::FORMAT_PKCS8));

License

SshKey is licensed under the MIT License.

Copyright 2014 Magnus Johansson

统计信息

  • 总下载量: 58.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 3
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固