KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import com.maverick.crypto.digests.SHA1Digest;
25 import java.math.BigInteger JavaDoc;
26 import com.maverick.crypto.security.SecureRandom;
27
28 public class RsaPrivateKey
29     extends RsaKey {
30
31   protected BigInteger JavaDoc privateExponent;
32
33   protected final static byte[] ASN_SHA1 = {
34       0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
35       0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
36   };
37   
38   public RsaPrivateKey(BigInteger JavaDoc modulus, BigInteger JavaDoc privateExponent) {
39     super(modulus);
40     this.privateExponent = privateExponent;
41   }
42
43   /* (non-Javadoc)
44  * @see com.maverick.crypto.publickey.RsaPrivateKeyInterface#getPrivateExponent()
45  */

46 public BigInteger JavaDoc getPrivateExponent() {
47     return privateExponent;
48   }
49
50   /* (non-Javadoc)
51  * @see com.maverick.crypto.publickey.RsaPrivateKeyInterface#sign(byte[])
52  */

53 public byte[] sign(byte[] msg) throws IOException JavaDoc {
54
55
56       SHA1Digest hash = new SHA1Digest();
57       hash.update(msg, 0, msg.length);
58
59       byte[] data = new byte[hash.getDigestSize()];
60       hash.doFinal(data, 0);
61
62       byte[] tmp = new byte[data.length + ASN_SHA1.length];
63       System.arraycopy(ASN_SHA1, 0, tmp, 0, ASN_SHA1.length);
64       System.arraycopy(data, 0, tmp, ASN_SHA1.length, data.length);
65       data = tmp;
66
67       BigInteger JavaDoc dataInt = new BigInteger JavaDoc(1, data);
68       int mLen = (getModulus().bitLength() + 7) / 8;
69
70       dataInt = Rsa.padPKCS1(dataInt, 1, mLen);
71
72       BigInteger JavaDoc signatureInt = null;
73
74       BigInteger JavaDoc privateExponent = getPrivateExponent();
75       BigInteger JavaDoc modulus = getModulus();
76       signatureInt = Rsa.doPrivate(dataInt,
77                                    modulus, privateExponent);
78
79       byte[] sig = unsignedBigIntToBytes(signatureInt, mLen);
80
81       return sig;
82
83   }
84
85   protected static byte[] unsignedBigIntToBytes(BigInteger JavaDoc bi, int size) {
86     byte[] tmp = bi.toByteArray();
87     byte[] tmp2 = null;
88     if (tmp.length > size) {
89       tmp2 = new byte[size];
90       System.arraycopy(tmp, tmp.length - size, tmp2, 0, size);
91     }
92     else if (tmp.length < size) {
93       tmp2 = new byte[size];
94       System.arraycopy(tmp, 0, tmp2, size - tmp.length, tmp.length);
95     }
96     else {
97       tmp2 = tmp;
98     }
99     return tmp2;
100   }
101
102   public boolean equals(Object JavaDoc obj) {
103     if (obj instanceof RsaPrivateKey) {
104       RsaPrivateKey key = (RsaPrivateKey) obj;
105       return key.getBitLength() == getBitLength()
106           && key.getModulus().compareTo(getModulus()) == 0
107           && key.getPrivateExponent().compareTo(getPrivateExponent()) == 0;
108     }
109     return false;
110   }
111 }
112
Popular Tags