KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > crypto > SigningProcessor


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladimir Radisic
5  * @Version 2.1.5
6  */

7
8
9 package org.enhydra.oyster.crypto;
10
11 import org.enhydra.oyster.exception.SMIMEException;
12 import org.enhydra.oyster.exception.ErrorStorage;
13 import java.security.PrivateKey JavaDoc;
14 import java.security.Signature JavaDoc;
15 import org.bouncycastle.jce.provider.JDKDigestSignature;
16
17
18 /**
19  * SigningProcessor class is used for signing input datas acording to given
20  * signature algorithm which (can be SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA
21  * or SHA1_WITH_DSA).
22  */

23 public class SigningProcessor {
24
25 /**
26  * For creating signature values necessary information are: data for signing as byte
27  * array, type of signing algorithm, and private key for performing of asymmetric
28  * encryption.
29  * @param forSigning0 information for signing
30  * @param key0 private key (DSA or RSA depend on type of signing)
31  * @param signAlg0 type of signing algorithm (can be SHA1_WITH_RSA, MD2_WITH_RSA,
32  * MD5_WITH_RSA or SHA1_WITH_DSA).
33  * @exception SMIMEException caused by non SMIMEException which can be one of the
34  * following: InvalidKeyException, SignatureException, NoSuchProviderException,
35  * NoSuchAlgorithmException.
36  */

37   public static byte[] getSignature (byte[] forSigning0, PrivateKey JavaDoc key0, String JavaDoc signAlg0) throws SMIMEException {
38     byte[] sVal = null;
39     try {
40       if (signAlg0.equalsIgnoreCase("SHA1_WITH_RSA")) {
41         JDKDigestSignature.SHA1WithRSAEncryption jd = new JDKDigestSignature.SHA1WithRSAEncryption();
42         jd.initSign(key0);
43         jd.update(forSigning0);
44         sVal = jd.sign();
45       }
46       else if (signAlg0.equalsIgnoreCase("SHA1_WITH_DSA")) {
47         Signature JavaDoc sig = Signature.getInstance("SHA1withDSA", "SUN");
48         sig.initSign(key0);
49         sig.update(forSigning0);
50         sVal = sig.sign();
51       }
52       else if (signAlg0.equalsIgnoreCase("MD2_WITH_RSA")) {
53         JDKDigestSignature.MD2WithRSAEncryption jd = new JDKDigestSignature.MD2WithRSAEncryption();
54         jd.initSign(key0);
55         jd.update(forSigning0);
56         sVal = jd.sign();
57       }
58       else if (signAlg0.equalsIgnoreCase("MD5_WITH_RSA")) {
59         JDKDigestSignature.MD5WithRSAEncryption jd = new JDKDigestSignature.MD5WithRSAEncryption();
60         jd.initSign(key0);
61         jd.update(forSigning0);
62         sVal = jd.sign();
63       }
64     }
65     catch(Exception JavaDoc e) {
66       throw SMIMEException.getInstance("org.enhydra.oyster.crypto.SigningProcessor",
67                                         e, "getSignature" );
68     }
69     return sVal;
70   }
71 }
72
73
74
75
Popular Tags