View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  	 * This method tests the existing rice keystore file
34  	 * 
35  	 * @throws Exception
36  	 */
37  	@Test public void testSigning() throws Exception {
38  		
39  		Config config = ConfigContext.getCurrentContextConfig();
40  //		config.parseConfig(); 
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  }