View Javadoc

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