1 package org.apache.turbine.services.crypto.provider;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
95 return new cryptix.tools.UnixCrypt(seed).crypt(value);
96 }
97
98 }