1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.messaging;
17
18 import org.junit.Test;
19 import org.kuali.rice.core.api.config.property.Config;
20 import org.kuali.rice.core.api.config.property.ConfigContext;
21 import org.kuali.rice.ksb.test.KSBTestCase;
22
23 import java.io.FileInputStream;
24 import java.security.KeyStore;
25 import java.security.PrivateKey;
26 import java.security.PublicKey;
27 import java.security.Signature;
28
29 public class DigitalSignatureTest extends KSBTestCase {
30
31
32
33
34
35
36
37 @Test public void testSigning() throws Exception {
38
39 Config config = ConfigContext.getCurrentContextConfig();
40
41
42 Signature rsa = Signature.getInstance("SHA1withRSA");
43 String keystoreLocation = config.getKeystoreFile();
44 String keystoreAlias = config.getKeystoreAlias();
45 String keystorePassword = config.getKeystorePassword();
46 KeyStore keystore = KeyStore.getInstance("JKS");
47 keystore.load(new FileInputStream(keystoreLocation), keystorePassword.toCharArray());
48 PrivateKey privateKey = (PrivateKey)keystore.getKey(keystoreAlias, keystorePassword.toCharArray());
49
50 rsa.initSign(privateKey);
51
52 String imLovinIt = "Ba-da-ba-ba-baa, I'm lovin' it!";
53 rsa.update(imLovinIt.getBytes());
54
55 byte[] sigToVerify = rsa.sign();
56
57
58 PublicKey publicKey = keystore.getCertificate(keystoreAlias).getPublicKey();
59 Signature verifySig = Signature.getInstance("SHA1withRSA");
60 verifySig.initVerify(publicKey);
61 verifySig.update(imLovinIt.getBytes());
62 boolean verifies = verifySig.verify(sigToVerify);
63 System.out.println("signature verifies: " + verifies);
64
65 }
66
67 }