001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.transport.publickey;
027:
028: import com.sshtools.j2ssh.util.Hash;
029:
030: import java.security.NoSuchAlgorithmException;
031:
032: /**
033: *
034: *
035: * @author $author$
036: * @version $Revision: 1.24 $
037: */
038: public abstract class SshPublicKey {
039: /**
040: *
041: *
042: * @return
043: */
044: public abstract String getAlgorithmName();
045:
046: /**
047: *
048: *
049: * @return
050: */
051: public abstract int getBitLength();
052:
053: /**
054: *
055: *
056: * @return
057: */
058: public abstract byte[] getEncoded();
059:
060: /**
061: *
062: *
063: * @return
064: */
065: public String getFingerprint() {
066: try {
067: Hash md5 = new Hash("MD5");
068: md5.putBytes(getEncoded());
069:
070: byte[] digest = md5.doFinal();
071: int bits = getBitLength();
072: bits = (((bits % 8) != 0) ? (bits += (bits % 8)) : bits);
073:
074: String ret = String.valueOf(bits);
075:
076: for (int i = 0; i < digest.length; i++) {
077: ret += (((i == 0) ? ":" : "") + " " + Integer
078: .toHexString(digest[i] & 0xFF));
079: }
080:
081: return ret;
082: } catch (NoSuchAlgorithmException nsae) {
083: return null;
084: }
085: }
086:
087: /**
088: *
089: *
090: * @param obj
091: *
092: * @return
093: */
094: public boolean equals(Object obj) {
095: if (obj instanceof SshPublicKey) {
096: return (getFingerprint().compareTo(
097: ((SshPublicKey) obj).getFingerprint()) == 0);
098: }
099:
100: return false;
101: }
102:
103: /**
104: *
105: *
106: * @return
107: */
108: public int hashCode() {
109: return getFingerprint().hashCode();
110: }
111:
112: /**
113: *
114: *
115: * @param signature
116: * @param exchangeHash
117: *
118: * @return
119: *
120: * @throws InvalidSshKeySignatureException
121: */
122: public abstract boolean verifySignature(byte[] signature,
123: byte[] exchangeHash) throws InvalidSshKeySignatureException;
124: }
|