HostKey.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00039 public class HostKey{
00040   private static final byte[] sshdss=Util.str2byte("ssh-dss");
00041   private static final byte[] sshrsa=Util.str2byte("ssh-rsa");
00042 
00044   protected static final int GUESS=0;
00046   public static final int SSHDSS=1;
00048   public static final int SSHRSA=2;
00050   static final int UNKNOWN=3;
00051 
00053   protected String host;
00055   protected int type;
00057   protected byte[] key;
00058 
00064   public HostKey(String host, byte[] key) throws JSchException {
00065     this(host, GUESS, key);
00066   }
00067 
00076   public HostKey(String host, int type, byte[] key) throws JSchException {
00077     this.host=host; 
00078     if(type==GUESS){
00079       if(key[8]=='d'){ this.type=SSHDSS; }
00080       else if(key[8]=='r'){ this.type=SSHRSA; }
00081       else { throw new JSchException("invalid key type");}
00082     }
00083     else{
00084       this.type=type; 
00085     }
00086     this.key=key;
00087   }
00088 
00092   public String getHost(){ return host; }
00093 
00097   public String getType(){
00098     // wouldn't    return "ssh-dss" etc. more clear? -- P.E.
00099     if(type==SSHDSS){ return Util.byte2str(sshdss); }
00100     if(type==SSHRSA){ return Util.byte2str(sshrsa);}
00101     return "UNKNOWN";
00102   }
00103 
00107   public String getKey(){
00108     return Util.byte2str(Util.toBase64(key, 0, key.length));
00109   }
00110 
00115   public String getFingerPrint(JSch jsch){
00116     HASH hash=null;
00117     try{
00118       Class c=Class.forName(jsch.getConfig("md5"));
00119       hash=(HASH)(c.newInstance());
00120     }
00121     catch(Exception e){ System.err.println("getFingerPrint: "+e); }
00122     return Util.getFingerPrint(hash, key);
00123   }
00124 
00128   boolean isMatched(String _host){
00129     return isIncluded(_host);
00130   }
00131 
00132   private boolean isIncluded(String _host){
00133     int i=0;
00134     String hosts=this.host; 
00135     int hostslen=hosts.length();
00136     int hostlen=_host.length();
00137     int j;
00138     while(i<hostslen){
00139       j=hosts.indexOf(',', i);
00140       if(j==-1){
00141        if(hostlen!=hostslen-i) return false;
00142        return hosts.regionMatches(true, i, _host, 0, hostlen);
00143       }
00144       if(hostlen==(j-i)){
00145     if(hosts.regionMatches(true, i, _host, 0, hostlen)) return true;
00146       }
00147       i=j+1;
00148     }
00149     return false;
00150   }
00151 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1