View Javadoc

1   package org.apache.turbine.services.crypto.provider;
2   
3   /*
4    * Copyright 2001-2005 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;
20  
21  import org.apache.commons.codec.binary.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 264148 2005-08-29 14:21:04Z henning $
41   */
42  public class JavaCrypt
43          implements CryptoAlgorithm
44  {
45  
46      /*** The default cipher */
47      public static final String DEFAULT_CIPHER = "SHA";
48  
49      /*** The cipher to use for encryption */
50      private String 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 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 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 encrypt(String value)
94              throws Exception
95      {
96          MessageDigest 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.encodeBase64(digest);
104         return (encodedDigest == null ? null : new String(encodedDigest));
105     }
106 
107 }