001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ksb.messaging;
017
018 import org.junit.Test;
019 import org.kuali.rice.core.api.config.property.Config;
020 import org.kuali.rice.core.api.config.property.ConfigContext;
021 import org.kuali.rice.ksb.test.KSBTestCase;
022
023 import java.io.FileInputStream;
024 import java.security.KeyStore;
025 import java.security.PrivateKey;
026 import java.security.PublicKey;
027 import java.security.Signature;
028
029 public class DigitalSignatureTest extends KSBTestCase {
030
031
032 /**
033 * This method tests the existing rice keystore file
034 *
035 * @throws Exception
036 */
037 @Test public void testSigning() throws Exception {
038
039 Config config = ConfigContext.getCurrentContextConfig();
040 // config.parseConfig();
041 //
042 Signature rsa = Signature.getInstance("SHA1withRSA");
043 String keystoreLocation = config.getKeystoreFile();
044 String keystoreAlias = config.getKeystoreAlias();
045 String keystorePassword = config.getKeystorePassword();
046 KeyStore keystore = KeyStore.getInstance("JKS");
047 keystore.load(new FileInputStream(keystoreLocation), keystorePassword.toCharArray());
048 PrivateKey privateKey = (PrivateKey)keystore.getKey(keystoreAlias, keystorePassword.toCharArray());
049
050 rsa.initSign(privateKey);
051
052 String imLovinIt = "Ba-da-ba-ba-baa, I'm lovin' it!";
053 rsa.update(imLovinIt.getBytes());
054
055 byte[] sigToVerify = rsa.sign();
056
057
058 PublicKey publicKey = keystore.getCertificate(keystoreAlias).getPublicKey();
059 Signature verifySig = Signature.getInstance("SHA1withRSA");
060 verifySig.initVerify(publicKey);
061 verifySig.update(imLovinIt.getBytes());
062 boolean verifies = verifySig.verify(sigToVerify);
063 System.out.println("signature verifies: " + verifies);
064
065 }
066
067 }