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 org.apache.turbine.services.crypto.CryptoAlgorithm;
20  
21  /***
22   * Implements Standard Unix crypt(3) for use with the Crypto Service.
23   *
24   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
25   * @version $Id: UnixCrypt.java 264148 2005-08-29 14:21:04Z henning $
26   */
27  public class UnixCrypt
28          implements CryptoAlgorithm
29  {
30  
31      /*** The seed to use */
32      private String seed = null;
33  
34      /*** standard Unix crypt chars (64) */
35      private static final char[] SALT_CHARS =
36              (("abcdefghijklmnopqrstuvwxyz"
37              + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./").toCharArray());
38  
39      /***
40       * C'tor
41       */
42      public UnixCrypt()
43      {
44      }
45  
46      /***
47       * This class never uses anything but
48       * UnixCrypt, so it is just a dummy
49       * (Fixme: Should we throw an exception if
50       * something is requested that we don't support?
51       *
52       * @param cipher    Cipher (ignored)
53       */
54      public void setCipher(String cipher)
55      {
56          /* dummy */
57      }
58  
59      /***
60       * Setting the seed for the UnixCrypt
61       * algorithm. If a null value is supplied,
62       * or no seed is set, then a random seed is used.
63       *
64       * @param seed     The seed value to use.
65       */
66      public void setSeed(String seed)
67      {
68          this.seed = seed;
69      }
70  
71      /***
72       * encrypt the supplied string with the requested cipher
73       *
74       * @param value       The value to be encrypted
75       * @return The encrypted value
76       * @throws Exception An Exception of the underlying implementation.
77       */
78      public String encrypt(String value)
79              throws Exception
80      {
81          if (seed == null)
82          {
83              java.util.Random randomGenerator = new java.util.Random();
84              int numSaltChars = SALT_CHARS.length;
85  
86              seed = (new StringBuffer())
87                      .append(SALT_CHARS[Math.abs(randomGenerator.nextInt())
88                      % numSaltChars])
89                      .append(SALT_CHARS[Math.abs(randomGenerator.nextInt())
90                      % numSaltChars])
91                      .toString();
92          }
93  
94          /* UnixCrypt seems to be a really widespread name... */
95          return new cryptix.tools.UnixCrypt(seed).crypt(value);
96      }
97  
98  }