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 * This is the Message Digest Implementation of Turbine 2.1. It does
31 * not pad the Base64 encryption of the Message Digests correctly but
32 * truncates after 20 chars. This leads to interoperability problems
33 * if you want to use e.g. database columns between two languages.
34 *
35 * If you upgrade an application from Turbine 2.1 and have already used
36 * the Security Service with encrypted passwords and no way to rebuild
37 * your databases, use this provider. It is bug-compatible.
38 *
39 * DO NOT USE THIS PROVIDER FOR ANY NEW APPLICATION!
40 *
41 * Nevertheless it can be used as the default crypto algorithm .
42 *
43 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44 * @version $Id: OldJavaCrypt.java 581797 2007-10-04 08:26:18Z sgoeschl $
45 */
46
47 public class OldJavaCrypt
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 OldJavaCrypt()
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
116 byte[] digest = md.digest(value.getBytes("UTF-8"));
117 byte[] base64 = Base64.encodeBase64(digest);
118
119
120 int len = (digest.length == 16 ? 20 : 24 );
121 byte[] result = new byte[len];
122 System.arraycopy(base64, 0, result, 0, result.length);
123 return new String(result);
124 }
125 }