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   * This is the Message Digest Implementation of Turbine 2.1. It does
31   * not pad the Base64 encryption of the Message Digests correctly but
32   * truncates after 20 chars. This leads to interoperability problems
33   * if you want to use e.g. database columns between two languages.
34   *
35   * If you upgrade an application from Turbine 2.1 and have already used
36   * the Security Service with encrypted passwords and no way to rebuild
37   * your databases, use this provider. It is bug-compatible.
38   *
39   * DO NOT USE THIS PROVIDER FOR ANY NEW APPLICATION!
40   *
41   * Nevertheless it can be used as the default crypto algorithm .
42   *
43   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44   * @version $Id: OldJavaCrypt.java 581797 2007-10-04 08:26:18Z sgoeschl $
45   */
46  
47  public class OldJavaCrypt
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 OldJavaCrypt()
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 
116         byte[] digest = md.digest(value.getBytes("UTF-8"));
117         byte[] base64 = Base64.encodeBase64(digest);
118         // from MD5 the digest has 16 bytes but for SHA1 it contains 20 bytes
119         // depending on the digest lenght the result is truncated
120         int len = (digest.length == 16 ? 20 : 24 );
121         byte[] result = new byte[len];
122         System.arraycopy(base64, 0, result, 0, result.length);
123         return new String(result);
124     }
125 }