01: /*
02: * Copyright 2007 The Kuali Foundation
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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 edu.iu.uis.eden.messaging;
17:
18: import java.io.FileInputStream;
19: import java.security.KeyStore;
20: import java.security.PrivateKey;
21: import java.security.PublicKey;
22: import java.security.Signature;
23:
24: import org.junit.Test;
25: import org.kuali.bus.test.KSBTestCase;
26: import org.kuali.rice.config.Config;
27: import org.kuali.rice.core.Core;
28:
29: public class DigitalSignatureTest extends KSBTestCase {
30:
31: @Test
32: public void testSigning() throws Exception {
33:
34: Config config = Core.getCurrentContextConfig();
35: // config.parseConfig();
36: //
37: Signature rsa = Signature.getInstance("SHA1withRSA");
38: String keystoreLocation = config.getKeystoreFile();
39: String keystoreAlias = config.getKeystoreAlias();
40: String keystorePassword = config.getKeystorePassword();
41: KeyStore keystore = KeyStore.getInstance("JKS");
42: keystore.load(new FileInputStream(keystoreLocation),
43: keystorePassword.toCharArray());
44: PrivateKey privateKey = (PrivateKey) keystore.getKey(
45: keystoreAlias, keystorePassword.toCharArray());
46:
47: rsa.initSign(privateKey);
48:
49: String imLovinIt = "Ba-da-ba-ba-baa, I'm lovin' it!";
50: rsa.update(imLovinIt.getBytes());
51:
52: byte[] sigToVerify = rsa.sign();
53:
54: PublicKey publicKey = keystore.getCertificate(keystoreAlias)
55: .getPublicKey();
56: Signature verifySig = Signature.getInstance("SHA1withRSA");
57: verifySig.initVerify(publicKey);
58: verifySig.update(imLovinIt.getBytes());
59: boolean verifies = verifySig.verify(sigToVerify);
60: System.out.println("signature verifies: " + verifies);
61:
62: }
63:
64: }
|