1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 package com.jcraft.jsch;
33 private static final byte[] sshdss=Util.str2byte("ssh-dss");
34 private static final byte[] sshrsa=Util.str2byte("ssh-rsa");
36 protected static final int GUESS=0;
37 public static final int SSHDSS=1;
38 public static final int SSHRSA=2;
39 static final int UNKNOWN=3;
41 protected String host;
45 public HostKey(String host, byte[] key) throws JSchException {
46 this(host, GUESS, key);
49 public HostKey(String host, int type, byte[] key) throws JSchException {
52 if(key[8]=='d'){ this.type=SSHDSS; }
53 else if(key[8]=='r'){ this.type=SSHRSA; }
54 else { throw new JSchException("invalid key type");}
62 public String getHost(){ return host; }
63 public String getType(){
64 if(type==SSHDSS){ return Util.byte2str(sshdss); }
65 if(type==SSHRSA){ return Util.byte2str(sshrsa);}
68 public String getKey(){
69 return Util.byte2str(Util.toBase64(key, 0, key.length));
71 public String getFingerPrint(JSch jsch){
74 Class c=Class.forName(jsch.getConfig("md5"));
75 hash=(HASH)(c.newInstance());
77 catch(Exception e){ System.err.println("getFingerPrint: "+e); }
78 return Util.getFingerPrint(hash, key);
81 boolean isMatched(String _host){
82 return isIncluded(_host);
85 private boolean isIncluded(String _host){
87 String hosts=this.host;
88 int hostslen=hosts.length();
89 int hostlen=_host.length();
92 j=hosts.indexOf(',', i);
94 if(hostlen!=hostslen-i) return false;
95 return hosts.regionMatches(true, i, _host, 0, hostlen);
98 if(hosts.regionMatches(true, i, _host, 0, hostlen)) return true;