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 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
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 }