File tree Expand file tree Collapse file tree 2 files changed +21
-16
lines changed Expand file tree Collapse file tree 2 files changed +21
-16
lines changed Original file line number Diff line number Diff line change 1010
1111class SSHConnection
1212{
13+ const FINGERPRINT_MD5 = 'md5 ' ;
14+ const FINGERPRINT_SHA1 = 'sha1 ' ;
15+
1316 private $ hostname ;
1417 private $ port = 22 ;
1518 private $ username ;
@@ -112,25 +115,21 @@ public function run(string $command): SSHCommand
112115 return new SSHCommand ($ this ->ssh , $ command );
113116 }
114117
115- public function md5Fingerprint (): string
116- {
117- return $ this ->getFingerprint (0 | 0 );
118- }
119-
120- public function sha1Fingerprint (): string
121- {
122- return $ this ->getFingerprint (0 | 0 );
123- }
124-
125- private function getFingerprint (int $ flags )
118+ public function fingerprint (string $ type = self ::FINGERPRINT_MD5 )
126119 {
127120 if (!$ this ->connected ) {
128121 throw new RuntimeException ('Unable to get fingerprint when not connected. ' );
129122 }
130123
131- $ hostkey = substr ($ this ->ssh ->getServerPublicHostKey (), 8 );
132- $ hostkey = ($ flags & 1 ) ? sha1 ($ hostkey ) : md5 ($ hostkey );
133- return ($ flags & 2 ) ? pack ('H* ' , $ hostkey ) : strtoupper ($ hostkey );
124+ switch ($ type ) {
125+ case 'md5 ' :
126+ return strtoupper (md5 ($ hostKey ));
127+
128+ case 'sha1 ' :
129+ return strtoupper (sha1 ($ hostKey ));
130+ }
131+
132+ throw new InvalidArgumentException ('Invalid fingerprint type specified. ' );
134133 }
135134
136135 public function upload (string $ localPath , string $ remotePath ): bool
Original file line number Diff line number Diff line change @@ -89,7 +89,10 @@ public function testMd5Fingerprint()
8989 ->withPrivateKey ('/home/travis/.ssh/id_rsa ' )
9090 ->connect ();
9191
92- $ this ->assertEquals ($ connection1 ->md5Fingerprint (), $ connection2 ->md5Fingerprint ());
92+ $ this ->assertEquals (
93+ $ connection1 ->fingerprint (SSHConnection::FINGERPRINT_MD5 ),
94+ $ connection2 ->fingerprint (SSHConnection::FINGERPRINT_MD5 )
95+ );
9396 }
9497
9598 public function testSha1Fingerprint ()
@@ -108,6 +111,9 @@ public function testSha1Fingerprint()
108111 ->withPrivateKey ('/home/travis/.ssh/id_rsa ' )
109112 ->connect ();
110113
111- $ this ->assertEquals ($ connection1 ->sha1Fingerprint (), $ connection2 ->sha1Fingerprint ());
114+ $ this ->assertEquals (
115+ $ connection1 ->fingerprint (SSHConnection::FINGERPRINT_SHA1 ),
116+ $ connection2 ->fingerprint (SSHConnection::FINGERPRINT_SHA1 )
117+ );
112118 }
113119}
You can’t perform that action at this time.
0 commit comments