Skip to content

Commit

Permalink
Merge 06cfcc2 into 261d2ba
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineOmega committed Oct 17, 2019
2 parents 261d2ba + 06cfcc2 commit f1cdf61
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ composer require divineomega/php-ssh-connection

## Usage

See the following basic usage instructions.

```php
$connection = (new SSHConnection())
->to('test.rebex.net')
Expand All @@ -32,3 +34,21 @@ $command->getError(); // ''
$connection->upload($localPath, $remotePath);
$connection->download($remotePath, $localPath);
```

For security, you can fingerprint the remote server and verify the fingerprint remain the same
upon each subsequent connection.

```php
$fingerprint = $connection->fingerprint();

if ($newConnection->fingerprint() != $fingerprint) {
throw new Exception('Fingerprint does not match!');
}
```

If you wish, you can specify the type of fingerprint you wish to retrieve.

```php
$md5Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_MD5); // default
$sha1Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA1);
```
22 changes: 22 additions & 0 deletions src/SSHConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class SSHConnection
{
const FINGERPRINT_MD5 = 'md5';
const FINGERPRINT_SHA1 = 'sha1';

private $hostname;
private $port = 22;
private $username;
Expand Down Expand Up @@ -112,6 +115,25 @@ public function run(string $command): SSHCommand
return new SSHCommand($this->ssh, $command);
}

public function fingerprint(string $type = self::FINGERPRINT_MD5)
{
if (!$this->connected) {
throw new RuntimeException('Unable to get fingerprint when not connected.');
}

$hostKey = substr($this->ssh->getServerPublicHostKey(), 8);

switch ($type) {
case 'md5':
return strtoupper(md5($hostKey));

case 'sha1':
return strtoupper(sha1($hostKey));
}

throw new InvalidArgumentException('Invalid fingerprint type specified.');
}

public function upload(string $localPath, string $remotePath): bool
{
if (!$this->connected) {
Expand Down
44 changes: 44 additions & 0 deletions tests/Integration/SSHConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,48 @@ public function testSSHConnectionWithPassword()
$this->assertEquals('', $command->getError());
$this->assertEquals('', $command->getRawError());
}

public function testMd5Fingerprint()
{
$connection1 = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKey('/home/travis/.ssh/id_rsa')
->connect();

$connection2 = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKey('/home/travis/.ssh/id_rsa')
->connect();

$this->assertEquals(
$connection1->fingerprint(SSHConnection::FINGERPRINT_MD5),
$connection2->fingerprint(SSHConnection::FINGERPRINT_MD5)
);
}

public function testSha1Fingerprint()
{
$connection1 = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKey('/home/travis/.ssh/id_rsa')
->connect();

$connection2 = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKey('/home/travis/.ssh/id_rsa')
->connect();

$this->assertEquals(
$connection1->fingerprint(SSHConnection::FINGERPRINT_SHA1),
$connection2->fingerprint(SSHConnection::FINGERPRINT_SHA1)
);
}
}

0 comments on commit f1cdf61

Please sign in to comment.