diff --git a/src/main/java/net/schmizz/sshj/transport/verification/OpenSSHKnownHosts.java b/src/main/java/net/schmizz/sshj/transport/verification/OpenSSHKnownHosts.java index 02ef7de41..7d71e1aa1 100644 --- a/src/main/java/net/schmizz/sshj/transport/verification/OpenSSHKnownHosts.java +++ b/src/main/java/net/schmizz/sshj/transport/verification/OpenSSHKnownHosts.java @@ -41,6 +41,7 @@ import java.security.PublicKey; import java.security.spec.RSAPublicKeySpec; import java.util.ArrayList; +import java.util.Arrays; import java.util.Base64; import java.util.List; @@ -468,7 +469,8 @@ public String getLine() { } private String getKeyString(PublicKey pk) { - return Base64.getEncoder().encodeToString(pk.getEncoded()); + final Buffer.PlainBuffer buf = new Buffer.PlainBuffer().putPublicKey(pk); + return Base64.getEncoder().encodeToString(Arrays.copyOfRange(buf.array(), buf.rpos(), buf.available())); } protected String getHostPart() { diff --git a/src/test/java/com/hierynomus/sshj/transport/verification/OpenSSHKnownHostsTest.java b/src/test/java/com/hierynomus/sshj/transport/verification/OpenSSHKnownHostsTest.java index d6c2f6056..e509656ea 100644 --- a/src/test/java/com/hierynomus/sshj/transport/verification/OpenSSHKnownHostsTest.java +++ b/src/test/java/com/hierynomus/sshj/transport/verification/OpenSSHKnownHostsTest.java @@ -63,6 +63,11 @@ public void shouldParseAndVerifyHashedHostEntry() throws Exception { OpenSSHKnownHosts ohk = new OpenSSHKnownHosts(knownHosts); assertTrue(ohk.verify("192.168.1.61", 22, k)); assertFalse(ohk.verify("192.168.1.2", 22, k)); + ohk.write(); + for (OpenSSHKnownHosts.KnownHostEntry entry : ohk.entries()) { + assertEquals("|1|F1E1KeoE/eEWhi10WpGv4OdiO6Y=|3988QV0VE8wmZL7suNrYQLITLCg= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6P9Hlwdahh250jGZYKg2snRq2j2lFJVdKSHyxqbJiVy9VX9gTkN3K2MD48qyrYLYOyGs3vTttyUk+cK++JMzURWsrP4piby7LpeOT+3Iq8CQNj4gXZdcH9w15Vuk2qS11at6IsQPVHpKD9HGg9//EFUccI/4w06k4XXLm/IxOGUwj6I2AeWmEOL3aDi+fe07TTosSdLUD6INtR0cyKsg0zC7Da24ixoShT8Oy3x2MpR7CY3PQ1pUVmvPkr79VeA+4qV9F1JM09WdboAMZgWQZ+XrbtuBlGsyhpUHSCQOya+kOJ+bYryS+U7A+6nmTW3C9FX4FgFqTF89UHOC7V0zZQ==", + entry.getLine()); + } } @Test