15
15
*/
16
16
package net .schmizz .sshj .transport .verification ;
17
17
18
- import java .io .IOException ;
19
18
import java .security .GeneralSecurityException ;
20
19
import java .security .MessageDigest ;
21
20
import java .security .PublicKey ;
22
21
import java .util .Arrays ;
22
+ import java .util .Base64 ;
23
23
import java .util .Collections ;
24
24
import java .util .List ;
25
25
import java .util .regex .Pattern ;
26
26
27
- import net .schmizz .sshj .common .Base64 ;
28
27
import net .schmizz .sshj .common .Buffer ;
29
28
import net .schmizz .sshj .common .SSHRuntimeException ;
30
29
import net .schmizz .sshj .common .SecurityUtils ;
@@ -46,48 +45,40 @@ public class FingerprintVerifier implements HostKeyVerifier {
46
45
*
47
46
* @param fingerprint of an SSH fingerprint in MD5 (hex), SHA-1 (base64) or SHA-256(base64) format
48
47
*
49
- * @return
48
+ * @return Host Key Verifier
50
49
*/
51
50
public static HostKeyVerifier getInstance (String fingerprint ) {
51
+ if (fingerprint .startsWith ("SHA1:" )) {
52
+ return new FingerprintVerifier ("SHA-1" , fingerprint .substring (5 ));
53
+ }
52
54
53
- try {
54
- if (fingerprint .startsWith ("SHA1:" )) {
55
- return new FingerprintVerifier ("SHA-1" , fingerprint .substring (5 ));
56
- }
55
+ if (fingerprint .startsWith ("SHA256:" )) {
56
+ return new FingerprintVerifier ("SHA-256" , fingerprint .substring (7 ));
57
+ }
57
58
58
- if (fingerprint .startsWith ("SHA256:" )) {
59
- return new FingerprintVerifier ("SHA-256" , fingerprint .substring (7 ));
60
- }
59
+ final String md5 ;
60
+ if (fingerprint .startsWith ("MD5:" )) {
61
+ md5 = fingerprint .substring (4 ); // remove the MD5: prefix
62
+ } else {
63
+ md5 = fingerprint ;
64
+ }
61
65
62
- final String md5 ;
63
- if (fingerprint .startsWith ("MD5:" )) {
64
- md5 = fingerprint .substring (4 ); // remove the MD5: prefix
65
- } else {
66
- md5 = fingerprint ;
67
- }
66
+ if (!MD5_FINGERPRINT_PATTERN .matcher (md5 ).matches ()) {
67
+ throw new SSHRuntimeException ("Invalid MD5 fingerprint: " + fingerprint );
68
+ }
68
69
69
- if (!MD5_FINGERPRINT_PATTERN .matcher (md5 ).matches ()) {
70
- throw new SSHRuntimeException ("Invalid MD5 fingerprint: " + fingerprint );
70
+ // Use the old default fingerprint verifier for md5 fingerprints
71
+ return (new HostKeyVerifier () {
72
+ @ Override
73
+ public boolean verify (String h , int p , PublicKey k ) {
74
+ return SecurityUtils .getFingerprint (k ).equals (md5 );
71
75
}
72
76
73
- // Use the old default fingerprint verifier for md5 fingerprints
74
- return (new HostKeyVerifier () {
75
- @ Override
76
- public boolean verify (String h , int p , PublicKey k ) {
77
- return SecurityUtils .getFingerprint (k ).equals (md5 );
78
- }
79
-
80
- @ Override
81
- public List <String > findExistingAlgorithms (String hostname , int port ) {
82
- return Collections .emptyList ();
83
- }
84
- });
85
- } catch (SSHRuntimeException e ) {
86
- throw e ;
87
- } catch (IOException e ) {
88
- throw new SSHRuntimeException (e );
89
- }
90
-
77
+ @ Override
78
+ public List <String > findExistingAlgorithms (String hostname , int port ) {
79
+ return Collections .emptyList ();
80
+ }
81
+ });
91
82
}
92
83
93
84
private final String digestAlgorithm ;
@@ -99,18 +90,16 @@ public List<String> findExistingAlgorithms(String hostname, int port) {
99
90
* the used digest algorithm
100
91
* @param base64Fingerprint
101
92
* base64 encoded fingerprint data
102
- *
103
- * @throws IOException
104
93
*/
105
- private FingerprintVerifier (String digestAlgorithm , String base64Fingerprint ) throws IOException {
94
+ private FingerprintVerifier (String digestAlgorithm , String base64Fingerprint ) {
106
95
this .digestAlgorithm = digestAlgorithm ;
107
96
108
97
// if the length is not padded with "=" chars at the end so that it is divisible by 4 the SSHJ Base64 implementation does not work correctly
109
98
StringBuilder base64FingerprintBuilder = new StringBuilder (base64Fingerprint );
110
99
while (base64FingerprintBuilder .length () % 4 != 0 ) {
111
100
base64FingerprintBuilder .append ("=" );
112
101
}
113
- fingerprintData = Base64 .decode (base64FingerprintBuilder .toString ());
102
+ fingerprintData = Base64 .getDecoder (). decode (base64FingerprintBuilder .toString ());
114
103
}
115
104
116
105
@ Override
0 commit comments