01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/user/tags/sakai_2-4-1/user-impl/impl/src/java/org/sakaiproject/user/impl/OneWayHash.java $
03: * $Id: OneWayHash.java 15514 2006-10-03 00:15:10Z ggolden@umich.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.user.impl;
21:
22: import java.io.ByteArrayOutputStream;
23: import java.io.OutputStream;
24: import java.security.MessageDigest;
25:
26: import javax.mail.internet.MimeUtility;
27:
28: import org.apache.commons.logging.Log;
29: import org.apache.commons.logging.LogFactory;
30:
31: /**
32: * <p>
33: * OneWayHash converts a plain text string into an encoded string.
34: * </p>
35: */
36: public class OneWayHash {
37: /** Our log (commons). */
38: private static Log M_log = LogFactory.getLog(OneWayHash.class);
39:
40: /**
41: * Encode the clear text into an encoded form.
42: *
43: * @param clear
44: * The text to encode.
45: * @param truncated
46: * return a value truncated as we used to be before fixing SAK-5922 (works only with MD5 algorithm base64'ed)
47: * @return The encoded and base64'ed text.
48: */
49: public static String encode(String clear, boolean truncated) {
50: try {
51: // compute the digest using the MD5 algorithm
52: MessageDigest md = MessageDigest.getInstance("MD5");
53: byte[] digest = md.digest(clear.getBytes("UTF-8"));
54:
55: // encode as base64
56: ByteArrayOutputStream bas = new ByteArrayOutputStream(
57: digest.length + digest.length / 3 + 1);
58: OutputStream encodedStream = MimeUtility.encode(bas,
59: "base64");
60: encodedStream.write(digest);
61:
62: // we used to pick up the encoding before it was complete, leaving off the last 4 characters of the encoded value
63: String truncatedValue = bas.toString();
64:
65: // close the stream to complete the encoding
66: encodedStream.close();
67: String rv = bas.toString();
68:
69: // if we are asking for the truncated value, return that
70: if (truncated)
71: return truncatedValue;
72:
73: return rv;
74: } catch (Exception e) {
75: M_log.warn("OneWayHash.encode: exception: " + e);
76: return null;
77: }
78: }
79: }
|