1 19 20 package com.maverick.crypto.publickey; 21 22 import java.io.IOException ; 23 24 import com.maverick.crypto.digests.SHA1Digest; 25 import java.math.BigInteger ; 26 27 28 public class DsaPublicKey 29 extends DsaKey implements PublicKey { 30 31 protected BigInteger y; 32 33 36 public DsaPublicKey(BigInteger p, 37 BigInteger q, 38 BigInteger g, 39 BigInteger y) { 40 super(p, q, g); 41 this.y = y; 42 } 43 44 public DsaPublicKey() { 45 46 } 47 48 public BigInteger getY() { 49 return y; 50 } 51 52 56 public int getBitLength() { 57 return p.bitLength(); 58 } 59 60 61 69 public boolean verifySignature(byte[] signature, byte[] msg) { 70 71 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 88 protected boolean verifySignature( 89 byte[] msg, 90 BigInteger r, 91 BigInteger s) { 92 93 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 m = new BigInteger (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 w = s.modInverse(q); 112 BigInteger u1 = m.multiply(w).mod(q); 113 BigInteger u2 = r.multiply(w).mod(q); 114 115 BigInteger 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 |