KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > SshPublicKey


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.pki;
21
22 import java.security.PublicKey JavaDoc;
23
24 import com.maverick.crypto.digests.Hash;
25
26
27 /**
28  *
29  *
30  * @author $author$
31  */

32 public abstract class SshPublicKey {
33     /**
34      *
35      *
36      * @return
37      */

38     public abstract String JavaDoc getAlgorithmName();
39
40     /**
41      *
42      *
43      * @return
44      */

45     public abstract int getBitLength();
46
47     
48     public abstract PublicKey JavaDoc getPublicKey();
49     
50     /**
51      *
52      *
53      * @return
54      */

55     public abstract byte[] getEncoded();
56
57     /**
58      *
59      *
60      * @return
61      */

62     public String JavaDoc getFingerprint() {
63             Hash md5 = new Hash("MD5");
64             md5.putBytes(getEncoded());
65
66             byte[] digest = md5.doFinal();
67             int bits = getBitLength();
68             bits = (((bits % 8) != 0) ? (bits += (bits % 8)) : bits);
69
70             String JavaDoc ret = String.valueOf(bits);
71
72             for (int i = 0; i < digest.length; i++) {
73                 ret += (((i == 0) ? ":" : "") + " " +
74                 Integer.toHexString(digest[i] & 0xFF));
75             }
76
77             return ret;
78     }
79
80     /**
81      *
82      *
83      * @param obj
84      *
85      * @return
86      */

87     public boolean equals(Object JavaDoc obj) {
88         if (obj instanceof SshPublicKey) {
89             return (getFingerprint().compareTo(((SshPublicKey) obj).getFingerprint()) == 0);
90         }
91
92         return false;
93     }
94
95     /**
96      *
97      *
98      * @return
99      */

100     public int hashCode() {
101         return getFingerprint().hashCode();
102     }
103
104     /**
105      *
106      *
107      * @param signature
108      * @param exchangeHash
109      *
110      * @return
111      *
112      * @throws InvalidSshKeySignatureException
113      */

114     public abstract boolean verifySignature(byte[] signature,
115         byte[] exchangeHash) throws InvalidSignatureException;
116 }
117
Popular Tags