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
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
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
98 return new cryptix.tools.UnixCrypt(seed).crypt(value);
99 }
100
101 }