1 package org.apache.fulcrum.crypto.provider;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }