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