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