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
24 import java.security.MessageDigest;
25
26 import org.apache.fulcrum.crypto.CryptoAlgorithm;
27 import org.apache.fulcrum.crypto.impl.Base64;
28
29 /**
30 * Implements the normal java.security.MessageDigest stream cipers.
31 * Base64 strings returned by this provider are correctly padded to
32 * multiples of four bytes. If you run into interoperability problems
33 * with other languages, especially perl and the Digest::MD5 module,
34 * note that the md5_base64 function from this package incorrectly drops
35 * the pad bytes. Use the MIME::Base64 package instead.
36 *
37 * If you upgrade from Turbine 2.1 and suddently your old stored passwords
38 * no longer work, please take a look at the OldJavaCrypt provider for
39 * bug-to-bug compatibility.
40 *
41 * This provider can be used as the default crypto algorithm provider.
42 *
43 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44 * @version $Id: JavaCrypt.java 581797 2007-10-04 08:26:18Z sgoeschl $
45 */
46
47 public class JavaCrypt
48 implements CryptoAlgorithm
49 {
50
51 /** The default cipher */
52 public static final String DEFAULT_CIPHER = "SHA";
53
54 /** The cipher to use for encryption */
55 private String cipher = null;
56
57
58 /**
59 * C'tor
60 *
61 */
62
63 public JavaCrypt()
64 {
65 this.cipher = DEFAULT_CIPHER;
66 }
67
68 /**
69 * Setting the actual cipher requested. If not
70 * called, then the default cipher (SHA) is used.
71 *
72 * This will never throw an error even if there is no
73 * provider for this cipher. The error will be thrown
74 * by encrypt() (Fixme?)
75 *
76 * @param cipher The cipher to use.
77 *
78 */
79
80 public void setCipher(String cipher)
81 {
82 this.cipher = cipher;
83 }
84
85 /**
86 * This class never uses a seed, so this is
87 * just a dummy.
88 *
89 * @param seed Seed (ignored)
90 *
91 */
92
93 public void setSeed(String seed)
94 {
95
96 }
97
98 /**
99 * encrypt the supplied string with the requested cipher
100 *
101 * @param value The value to be encrypted
102 *
103 * @return The encrypted value
104 *
105 * @throws Exception An Exception of the underlying implementation.
106 */
107
108 public String encrypt(String value)
109 throws Exception
110 {
111 MessageDigest md = MessageDigest.getInstance(cipher);
112
113
114
115 byte[] digest = md.digest(value.getBytes("UTF-8"));
116
117
118 byte[] encodedDigest = Base64.encodeBase64(digest);
119 return (encodedDigest == null ? null :
120 new String(encodedDigest));
121 }
122 }