diff options
author | Mike Lockwood <lockwood@google.com> | 2012-01-10 14:24:22 -0800 |
---|---|---|
committer | Mike Lockwood <lockwood@google.com> | 2012-01-10 14:25:05 -0800 |
commit | 48ded2421114c4c87ef3f8005c9f793a5d077cbd (patch) | |
tree | be5d899a8b290aa1247c86a5fa57498905d5c584 /examples/SimpleVerifier.java | |
parent | dca8ebfb5703cd5d0f6f60e9e9d52854ff13aaf5 (diff) | |
download | ganymed-ssh2-48ded2421114c4c87ef3f8005c9f793a5d077cbd.tar.gz |
ganymed-ssh2-build251beta1
Signed-off-by: Mike Lockwood <lockwood@google.com>
Diffstat (limited to 'examples/SimpleVerifier.java')
-rw-r--r-- | examples/SimpleVerifier.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/examples/SimpleVerifier.java b/examples/SimpleVerifier.java new file mode 100644 index 0000000..6ce67a2 --- /dev/null +++ b/examples/SimpleVerifier.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2006-2011 Christian Plattner. All rights reserved. + * Please refer to the LICENSE.txt for licensing details. + */ +import ch.ethz.ssh2.KnownHosts; +import ch.ethz.ssh2.ServerHostKeyVerifier; + +class SimpleVerifier implements ServerHostKeyVerifier +{ + KnownHosts database; + + /* + * This class is being used by the UsingKnownHosts.java example. + */ + + public SimpleVerifier(KnownHosts database) + { + if (database == null) + throw new IllegalArgumentException(); + + this.database = database; + } + + public boolean verifyServerHostKey(String hostname, int port, String serverHostKeyAlgorithm, byte[] serverHostKey) + throws Exception + { + int result = database.verifyHostkey(hostname, serverHostKeyAlgorithm, serverHostKey); + + switch (result) + { + case KnownHosts.HOSTKEY_IS_OK: + + return true; // We are happy + + case KnownHosts.HOSTKEY_IS_NEW: + + // Unknown host? Blindly accept the key and put it into the cache. + // Well, you definitely can do better (e.g., ask the user). + + // The following call will ONLY put the key into the memory cache! + // To save it in a known hosts file, also call "KnownHosts.addHostkeyToFile(...)" + database.addHostkey(new String[] { hostname }, serverHostKeyAlgorithm, serverHostKey); + + return true; + + case KnownHosts.HOSTKEY_HAS_CHANGED: + + // Close the connection if the hostkey has changed. + // Better: ask user and add new key to database. + return false; + + default: + throw new IllegalStateException(); + } + } +}
\ No newline at end of file |