KickJava   Java API By Example, From Geeks To Geeks.

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


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
27
28 public class DsaPublicKey
29     extends DsaKey implements PublicKey {
30
31   protected BigInteger JavaDoc y;
32
33   /**
34    * Contruct an uninitialized DSA public key
35    */

36   public DsaPublicKey(BigInteger JavaDoc p,
37                       BigInteger JavaDoc q,
38                       BigInteger JavaDoc g,
39                       BigInteger JavaDoc y) {
40     super(p, q, g);
41     this.y = y;
42   }
43
44   public DsaPublicKey() {
45
46   }
47
48   public BigInteger JavaDoc getY() {
49     return y;
50   }
51
52   /**
53    * Get the bit length of this key.
54    * @return the bit length of key.
55    */

56   public int getBitLength() {
57     return p.bitLength();
58   }
59
60
61   /**
62    * Verify the signature of the data to determine whether the signature was
63    * produced by the corresponding private key.
64    * @param signature
65    * @param data
66    * @return <code>true</code> if the signature is valid, otherwise <code>false</code>
67    * @throws IOException
68    */

69   public boolean verifySignature(byte[] signature, byte[] msg) {
70
71       // Create a SHA1 hash of the message
72
SHA1Digest h = new SHA1Digest();
73       h.update(msg, 0, msg.length);
74       byte[] data = new byte[h.getDigestSize()];
75       h.doFinal(data, 0);
76
77       return Dsa.verify(y, p, q, g, signature, data);
78   }
79
80   /**
81    * return true if the value r and s represent a DSA signature for
82    * the passed in message for standard DSA the message should be a
83    * SHA-1 hash of the real message to be verified.
84    *
85    * @return <code>true</code> if the values of r and s represent a DSA
86    * signature for the passed in message, otherwise <code>false</code>.
87    */

88   protected boolean verifySignature(
89       byte[] msg,
90       BigInteger JavaDoc r,
91       BigInteger JavaDoc s) {
92
93     // Create a SHA1 hash of the message
94
SHA1Digest h = new SHA1Digest();
95     h.update(msg, 0, msg.length);
96     byte[] data = new byte[h.getDigestSize()];
97     h.doFinal(data, 0);
98
99
100     BigInteger JavaDoc m = new BigInteger JavaDoc(1, data);
101     m = m.mod(q);
102
103     if (BigInteger.valueOf(0).compareTo(r) >= 0 || q.compareTo(r) <= 0) {
104       return false;
105     }
106
107     if (BigInteger.valueOf(0).compareTo(s) >= 0 || q.compareTo(s) <= 0) {
108       return false;
109     }
110
111     BigInteger JavaDoc w = s.modInverse(q);
112     BigInteger JavaDoc u1 = m.multiply(w).mod(q);
113     BigInteger JavaDoc u2 = r.multiply(w).mod(q);
114
115     BigInteger JavaDoc v = g.modPow(u1, p).multiply(y.modPow(u2, p)).mod(p).mod(q);
116
117     return (v.compareTo(r) == 0);
118
119   }
120 }
121
Popular Tags