FingerPrintVerifier implements HostKeyVerifier { @Override public boolean verify( String hostname, int port, PublicKey key ) { String expectedFingerPrint = this.targetProperties.get( EmbeddedHandler.SCP_HOST_KEY_PREFIX + hostname ); String fingerPrint = SecurityUtils.getFingerprint( key ); return Objects.equals( expectedFingerPrint, fingerPrint ); } FingerPrintVerifier( Map<String,String> targetProperties ); @Override boolean verify( String hostname, int port, PublicKey key );  }
@Test public void testVerify() throws Exception { final String ip1 = "ip1"; final String ip2 = "ip2"; Map<String,String> targetProperties = new HashMap<> (); FingerPrintVerifier verifier = new FingerPrintVerifier( targetProperties ); KeyPairGenerator keyGen = KeyPairGenerator.getInstance( "RSA" ); PublicKey publicKey1 = keyGen.generateKeyPair().getPublic(); PublicKey publicKey2 = keyGen.generateKeyPair().getPublic(); PublicKey publicKey3 = keyGen.generateKeyPair().getPublic(); Assert.assertNotEquals( publicKey1, publicKey2 ); Assert.assertNotEquals( publicKey3, publicKey2 ); Assert.assertNotEquals( publicKey3, publicKey1 ); targetProperties.put( EmbeddedHandler.SCP_HOST_KEY_PREFIX + ip1, SecurityUtils.getFingerprint( publicKey1 )); targetProperties.put( EmbeddedHandler.SCP_HOST_KEY_PREFIX + ip2, SecurityUtils.getFingerprint( publicKey2 )); Assert.assertTrue( verifier.verify( ip1, 22, publicKey1 )); Assert.assertTrue( verifier.verify( ip2, 22, publicKey2 )); Assert.assertFalse( verifier.verify( ip1, 22, publicKey2 )); Assert.assertFalse( verifier.verify( ip2, 2215, publicKey3 )); }