KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleVerifier


1 import ch.ethz.ssh2.KnownHosts;
2 import ch.ethz.ssh2.ServerHostKeyVerifier;
3
4 class SimpleVerifier implements ServerHostKeyVerifier
5 {
6     KnownHosts database;
7
8     /*
9      * This class is being used by the UsingKnownHosts.java example.
10      */

11     
12     public SimpleVerifier(KnownHosts database)
13     {
14         if (database == null)
15             throw new IllegalArgumentException JavaDoc();
16
17         this.database = database;
18     }
19
20     public boolean verifyServerHostKey(String JavaDoc hostname, int port, String JavaDoc serverHostKeyAlgorithm, byte[] serverHostKey)
21             throws Exception JavaDoc
22     {
23         int result = database.verifyHostkey(hostname, serverHostKeyAlgorithm, serverHostKey);
24
25         switch (result)
26         {
27         case KnownHosts.HOSTKEY_IS_OK:
28
29             return true; // We are happy
30

31         case KnownHosts.HOSTKEY_IS_NEW:
32
33             // Unknown host? Blindly accept the key and put it into the cache.
34
// Well, you definitely can do better (e.g., ask the user).
35

36             // The following call will ONLY put the key into the memory cache!
37
// To save it in a known hosts file, also call "KnownHosts.addHostkeyToFile(...)"
38
database.addHostkey(new String JavaDoc[] { hostname }, serverHostKeyAlgorithm, serverHostKey);
39
40             return true;
41
42         case KnownHosts.HOSTKEY_HAS_CHANGED:
43
44             // Close the connection if the hostkey has changed.
45
// Better: ask user and add new key to database.
46
return false;
47
48         default:
49             throw new IllegalStateException JavaDoc();
50         }
51     }
52 }
Popular Tags