View Javadoc

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