View Javadoc

1   package org.apache.turbine.services.crypto.provider;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.security.MessageDigest;
23  
24  import org.apache.commons.codec.binary.Base64;
25  
26  import org.apache.turbine.services.crypto.CryptoAlgorithm;
27  
28  /***
29   * Implements the normal java.security.MessageDigest stream cipers.
30   * Base64 strings returned by this provider are correctly padded to
31   * multiples of four bytes. If you run into interoperability problems
32   * with other languages, especially perl and the Digest::MD5 module,
33   * note that the md5_base64 function from this package incorrectly drops
34   * the pad bytes. Use the MIME::Base64 package instead.
35   *
36   * If you upgrade from Turbine 2.1 and suddently your old stored passwords
37   * no longer work, please take a look at the OldJavaCrypt provider for
38   * bug-to-bug compatibility.
39   *
40   * This provider can be used as the default crypto algorithm provider.
41   *
42   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43   * @version $Id: JavaCrypt.java 534527 2007-05-02 16:10:59Z tv $
44   */
45  public class JavaCrypt
46          implements CryptoAlgorithm
47  {
48  
49      /*** The default cipher */
50      public static final String DEFAULT_CIPHER = "SHA";
51  
52      /*** The cipher to use for encryption */
53      private String cipher = null;
54  
55      /***
56       * C'tor
57       */
58      public JavaCrypt()
59      {
60          this.cipher = DEFAULT_CIPHER;
61      }
62  
63      /***
64       * Setting the actual cipher requested. If not
65       * called, then the default cipher (SHA) is used.
66       *
67       * This will never throw an error even if there is no
68       * provider for this cipher. The error will be thrown
69       * by encrypt() (Fixme?)
70       *
71       * @param cipher     The cipher to use.
72       */
73      public void setCipher(String cipher)
74      {
75          this.cipher = cipher;
76      }
77  
78      /***
79       * This class never uses a seed, so this is
80       * just a dummy.
81       *
82       * @param seed        Seed (ignored)
83       */
84      public void setSeed(String seed)
85      {
86          /* dummy */
87      }
88  
89      /***
90       * encrypt the supplied string with the requested cipher
91       *
92       * @param value       The value to be encrypted
93       * @return The encrypted value
94       * @throws Exception An Exception of the underlying implementation.
95       */
96      public String encrypt(String value)
97              throws Exception
98      {
99          MessageDigest md = MessageDigest.getInstance(cipher);
100 
101         // We need to use unicode here, to be independent of platform's
102         // default encoding. Thanks to SGawin for spotting this.
103         byte[] digest = md.digest(value.getBytes("UTF-8"));
104 
105         // Base64-encode the digest.
106         byte[] encodedDigest = Base64.encodeBase64(digest);
107         return (encodedDigest == null ? null : new String(encodedDigest));
108     }
109 
110 }