1 2 29 30 package com.jcraft.jsch; 31 32 public class HostKey{ 33 private static final byte[] sshdss="ssh-dss".getBytes(); 34 private static final byte[] sshrsa="ssh-rsa".getBytes(); 35 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; 40 41 protected String host; 42 protected int type; 43 protected byte[] key; 44 45 public HostKey(String host, byte[] key) throws JSchException { 46 this(host, GUESS, key); 47 } 48 49 public HostKey(String host, int type, byte[] key) throws JSchException { 50 this.host=host; 51 if(type==GUESS){ 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");} 55 } 56 else{ 57 this.type=type; 58 } 59 this.key=key; 60 } 61 62 public String getHost(){ return host; } 63 public String getType(){ 64 if(type==SSHDSS){ return new String (sshdss); } 65 if(type==SSHRSA){ return new String (sshrsa);} 66 return "UNKNOWN"; 67 } 68 public String getKey(){ 69 return new String (Util.toBase64(key, 0, key.length)); 70 } 71 public String getFingerPrint(JSch jsch){ 72 HASH hash=null; 73 try{ 74 Class c=Class.forName(jsch.getConfig("md5")); 75 hash=(HASH)(c.newInstance()); 76 } 77 catch(Exception e){ System.err.println("getFingerPrint: "+e); } 78 return Util.getFingerPrint(hash, key); 79 } 80 81 boolean isMatched(String _host){ 82 return isIncluded(_host); 83 } 84 85 private boolean isIncluded(String _host){ 86 int i=0; 87 String hosts=this.host; 88 int hostslen=hosts.length(); 89 int hostlen=_host.length(); 90 int j; 91 while(i<hostslen){ 92 j=hosts.indexOf(',', i); 93 if(j==-1){ 94 if(hostlen!=hostslen-i) return false; 95 return hosts.regionMatches(true, i, _host, 0, hostlen); 96 } 97 if(hostlen==(j-i)){ 98 if(hosts.regionMatches(true, i, _host, 0, hostlen)) return true; 99 } 100 i=j+1; 101 } 102 return false; 103 } 104 } 105 | Popular Tags |