View Javadoc
1   package civitas.crypto.rsapublickey;
2   
3   import java.security.PublicKey;
4   
5   import org.bouncycastle.crypto.CryptoException;
6   import org.springframework.beans.factory.annotation.Autowired;
7   import org.springframework.stereotype.Controller;
8   
9   import civitas.crypto.Constants;
10  import civitas.crypto.CryptoBase;
11  import civitas.crypto.messagedigest.CryptoHash;
12  import civitas.crypto.signature.Signature;
13  
14  @Controller
15  public class VerifyPublicKeySignature implements Constants {
16  	@Autowired
17  	CryptoHash cryptoHash;
18  
19  	@Autowired
20  	CryptoBase cryptoBase;
21  
22  	@Autowired
23  	ConvertStringToPublicKey convertStringToPublicKey;
24  
25  	public boolean apply(final Signature s, final String msg) throws CryptoException {
26  		byte[] bytes = cryptoHash.apply(msg.getBytes());
27  		return apply(s, bytes);
28  	}
29  
30  	public boolean apply(final Signature s, final PublicKey pubKey, final String msg) throws CryptoException {
31  		byte[] bytes = cryptoHash.apply(msg.getBytes());
32  		return apply(s, pubKey, bytes);
33  	}
34  
35  	public boolean apply(final Signature s, final byte[] bytes) throws CryptoException {
36  		String pubkeyString = s.getSignerPubKey();
37  		PublicKey pubKey = convertStringToPublicKey.apply(pubkeyString);
38  		return apply(s, pubKey, bytes);
39  	}
40  
41  	public boolean apply(final Signature s, final PublicKey signer, final byte[] bytes) throws CryptoException {
42  		try {
43  			cryptoBase.rsaSigner.initVerify(signer);
44  			cryptoBase.rsaSigner.update(bytes);
45  			return cryptoBase.rsaSigner.verify(s.signatureBytes);
46  		} catch (Exception e) {
47  			throw new CryptoException("cannot verify signature", e);
48  		}
49  	}
50  }