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