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.io.ByteArrayOutputStream;
23  import java.io.OutputStream;
24  
25  import java.security.MessageDigest;
26  
27  import javax.mail.internet.MimeUtility;
28  
29  import org.apache.turbine.services.crypto.CryptoAlgorithm;
30  
31  /***
32   * This is the Message Digest Implementation of Turbine 2.1. It does
33   * not pad the Base64 encryption of the Message Digests correctly but
34   * truncates after 20 chars. This leads to interoperability problems
35   * if you want to use e.g. database columns between two languages.
36   *
37   * If you upgrade an application from Turbine 2.1 and have already used
38   * the Security Service with encrypted passwords and no way to rebuild
39   * your databases, use this provider. It is bug-compatible.
40   *
41   * DO NOT USE THIS PROVIDER FOR ANY NEW APPLICATION!
42   *
43   * Nevertheless it can be used as the default crypto algorithm .
44   *
45   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
46   * @version $Id: OldJavaCrypt.java 534527 2007-05-02 16:10:59Z tv $
47   */
48  public class OldJavaCrypt
49          implements CryptoAlgorithm
50  {
51  
52      /*** The default cipher */
53      public static final String DEFAULT_CIPHER = "SHA";
54  
55      /*** The cipher to use for encryption */
56      private String cipher = null;
57  
58      /***
59       * C'tor
60       */
61      public OldJavaCrypt()
62      {
63          this.cipher = DEFAULT_CIPHER;
64      }
65  
66      /***
67       * Setting the actual cipher requested. If not
68       * called, then the default cipher (SHA) is used.
69       *
70       * This will never throw an error even if there is no
71       * provider for this cipher. The error will be thrown
72       * by encrypt() (Fixme?)
73       *
74       * @param cipher     The cipher to use.
75       */
76      public void setCipher(String cipher)
77      {
78          this.cipher = cipher;
79      }
80  
81      /***
82       * This class never uses a seed, so this is
83       * just a dummy.
84       *
85       * @param seed        Seed (ignored)
86       */
87      public void setSeed(String seed)
88      {
89          /* dummy */
90      }
91  
92      /***
93       * Encrypt the supplied string with the requested cipher
94       *
95       * @param value       The value to be encrypted
96       * @return The encrypted value
97       * @throws Exception An Exception of the underlying implementation.
98       */
99      public String encrypt(String value)
100             throws Exception
101     {
102         MessageDigest md = MessageDigest.getInstance(cipher);
103 
104         // We need to use unicode here, to be independent of platform's
105         // default encoding. Thanks to SGawin for spotting this.
106 
107         byte[] digest = md.digest(value.getBytes("UTF-8"));
108         ByteArrayOutputStream bas =
109                 new ByteArrayOutputStream(digest.length + digest.length / 3 + 1);
110         OutputStream encodedStream = MimeUtility.encode(bas, "base64");
111         encodedStream.write(digest);
112         return bas.toString();
113     }
114 
115 }