1 19 20 package com.maverick.crypto.publickey; 21 22 import java.io.IOException ; 23 import com.maverick.crypto.digests.SHA1Digest; 24 import java.math.BigInteger ; 25 26 27 public class RsaPublicKey 28 extends RsaKey implements PublicKey { 29 30 protected BigInteger 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 modulus, BigInteger publicExponent) { 41 super(modulus); 42 this.publicExponent = publicExponent; 43 } 44 45 public BigInteger getPublicExponent() { 46 return publicExponent; 47 } 48 49 protected void setPublicExponent(BigInteger publicExponent) { 50 this.publicExponent = publicExponent; 51 } 52 53 public boolean verifySignature(byte[] signature, byte[] msg) { 54 55 BigInteger signatureInt = new BigInteger (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 |