KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > publickey > RsaPublicKey


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.maverick.crypto.publickey;
21
22 import java.io.IOException JavaDoc;
23 import com.maverick.crypto.digests.SHA1Digest;
24 import java.math.BigInteger JavaDoc;
25
26
27 public class RsaPublicKey
28     extends RsaKey implements PublicKey {
29
30   protected BigInteger JavaDoc publicExponent;
31
32   protected final static byte[] ASN_SHA1 = {
33       0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
34       0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
35   };
36   
37   public RsaPublicKey() {
38   }
39   
40   public RsaPublicKey(BigInteger JavaDoc modulus, BigInteger JavaDoc publicExponent) {
41     super(modulus);
42     this.publicExponent = publicExponent;
43   }
44
45   public BigInteger JavaDoc getPublicExponent() {
46     return publicExponent;
47   }
48   
49   protected void setPublicExponent(BigInteger JavaDoc publicExponent) {
50       this.publicExponent = publicExponent;
51   }
52
53   public boolean verifySignature(byte[] signature, byte[] msg) {
54
55       BigInteger JavaDoc signatureInt = new BigInteger JavaDoc(1, signature);
56
57       signatureInt = Rsa.doPublic(signatureInt,
58                                   getModulus(), publicExponent);
59
60       signatureInt = Rsa.removePKCS1(signatureInt, 1);
61
62       signature = signatureInt.toByteArray();
63
64       SHA1Digest h = new SHA1Digest();
65       h.update(msg, 0, msg.length);
66       byte[] data = new byte[h.getDigestSize()];
67       h.doFinal(data, 0);
68
69       if(data.length != (signature.length - ASN_SHA1.length)) {
70         return false;
71       }
72
73       byte[] cmp = ASN_SHA1;
74       for(int i = 0, j = 0; i < signature.length; i++, j++) {
75         if(i == ASN_SHA1.length) {
76           cmp = data;
77           j = 0;
78         }
79         if(signature[i] != cmp[j]) {
80           return false;
81         }
82       }
83       return true;
84
85   }
86
87   public int hashCode() {
88
89       int hashCode = getModulus().hashCode();
90
91       hashCode ^= publicExponent.hashCode();
92
93       return hashCode;
94   }
95
96
97 }
98
Popular Tags