KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > crypto > provider > JavaCrypt


1 package org.apache.turbine.services.crypto.provider;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.security.MessageDigest JavaDoc;
20
21 import org.apache.commons.codec.base64.Base64;
22
23 import org.apache.turbine.services.crypto.CryptoAlgorithm;
24
25 /**
26  * Implements the normal java.security.MessageDigest stream cipers.
27  * Base64 strings returned by this provider are correctly padded to
28  * multiples of four bytes. If you run into interoperability problems
29  * with other languages, especially perl and the Digest::MD5 module,
30  * note that the md5_base64 function from this package incorrectly drops
31  * the pad bytes. Use the MIME::Base64 package instead.
32  *
33  * If you upgrade from Turbine 2.1 and suddently your old stored passwords
34  * no longer work, please take a look at the OldJavaCrypt provider for
35  * bug-to-bug compatibility.
36  *
37  * This provider can be used as the default crypto algorithm provider.
38  *
39  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40  * @version $Id: JavaCrypt.java,v 1.5.2.2 2004/05/20 03:05:19 seade Exp $
41  */

42 public class JavaCrypt
43         implements CryptoAlgorithm
44 {
45
46     /** The default cipher */
47     public static final String JavaDoc DEFAULT_CIPHER = "SHA";
48
49     /** The cipher to use for encryption */
50     private String JavaDoc cipher = null;
51
52     /**
53      * C'tor
54      */

55     public JavaCrypt()
56     {
57         this.cipher = DEFAULT_CIPHER;
58     }
59
60     /**
61      * Setting the actual cipher requested. If not
62      * called, then the default cipher (SHA) is used.
63      *
64      * This will never throw an error even if there is no
65      * provider for this cipher. The error will be thrown
66      * by encrypt() (Fixme?)
67      *
68      * @param cipher The cipher to use.
69      */

70     public void setCipher(String JavaDoc cipher)
71     {
72         this.cipher = cipher;
73     }
74
75     /**
76      * This class never uses a seed, so this is
77      * just a dummy.
78      *
79      * @param seed Seed (ignored)
80      */

81     public void setSeed(String JavaDoc seed)
82     {
83         /* dummy */
84     }
85
86     /**
87      * encrypt the supplied string with the requested cipher
88      *
89      * @param value The value to be encrypted
90      * @return The encrypted value
91      * @throws Exception An Exception of the underlying implementation.
92      */

93     public String JavaDoc encrypt(String JavaDoc value)
94             throws Exception JavaDoc
95     {
96         MessageDigest JavaDoc md = MessageDigest.getInstance(cipher);
97
98         // We need to use unicode here, to be independent of platform's
99
// default encoding. Thanks to SGawin for spotting this.
100
byte[] digest = md.digest(value.getBytes("UTF-8"));
101
102         // Base64-encode the digest.
103
byte[] encodedDigest = Base64.encode(digest);
104         return (encodedDigest == null ? null : new String JavaDoc(encodedDigest));
105     }
106     
107 }
108
Popular Tags