/**********************************************************************************
*
* $Header: /cvs/sakai/legacy-component/src/java/org/sakaiproject/component/legacy/user/OneWayHash.java,v 1.6 2004/06/22 03:13:55 ggolden Exp $
*
***********************************************************************************
*
* Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
*                  Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
* 
* Licensed under the Educational Community License Version 1.0 (the "License");
* By obtaining, using and/or copying this Original Work, you agree that you have read,
* understand, and will comply with the terms and conditions of the Educational Community License.
* You may obtain a copy of the License at:
* 
*      http://cvs.sakaiproject.org/licenses/license_1_0.html
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**********************************************************************************/

// This is a hack so we can get access ot OneWayHash because it is not a share element
// So we put a copy here as well
// If we can make the semantics this service needs  work, then AUTHN is quite powerful :) 
// But in the short term it needs to talk to legacy only and even then sneak in the back door 
// a bit.

// package
package org.sakaiproject.component.grid;
// package org.sakaiproject.component.legacy.user;

// imports
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.security.MessageDigest;

import javax.mail.internet.MimeUtility;

import org.sakaiproject.service.framework.log.cover.Logger;

/**
* <p>OneWayHash converts a plain text string into an encoded string.</p>
* 
* @author University of Michigan, CHEF Software Development Team
* @version $Revision: 1.6 $
*/
public class OneWayHash
{
	/**
	* Encode the clear text into an encoded form.
	* @param clear The text to encode.
	* @return The encoded text.
	*/
	public static String encode(String clear)
	{
        try
        {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(clear.getBytes("UTF-8"));
            ByteArrayOutputStream bas = new ByteArrayOutputStream(digest.length + digest.length / 3 + 1);
            OutputStream encodedStream = MimeUtility.encode(bas, "base64");
            encodedStream.write(digest);
            return bas.toString();
        }
        catch (Exception e)
        {
            Logger.warn("OneWayHash.encode: exception: " + e);
            return null;
        }

	}	// encode

	/*
	public static void main(String args[])
	{
		String encoded = OneWayHash.encode("admin");
		System.out.println("admin = " + encoded);
		
		encoded = OneWayHash.encode("");
		System.out.println(" = " + encoded);

	}	// main
	*/

}	// OneWayHash

/**********************************************************************************
*
* $Header: /cvs/sakai/legacy-component/src/java/org/sakaiproject/component/legacy/user/OneWayHash.java,v 1.6 2004/06/22 03:13:55 ggolden Exp $
*
**********************************************************************************/
