/*

 Md5.java

 Copyright (c) 2008 Nick Ivanov / www.datori.org
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 This Java class implements a single method that computes an MD5 hash value
 of a CLOB object passed to it. The hash value is returned as a 32-character
 hexadecimal string.

 The class is intended to be used as a DB2 user-defined function.

*/
import java.security.*;
import COM.ibm.db2.app.UDF;
import COM.ibm.db2.app.Clob;
import java.io.StringReader;
import java.io.BufferedReader;

public class Md5 extends UDF
{

	public void md5(Clob sqltext, String out) throws java.sql.SQLException
	{
		try{
			 MessageDigest algorithm = MessageDigest.getInstance("MD5");
			 algorithm.reset();

			 BufferedReader clobReader = new BufferedReader(sqltext.getReader());

			 int ch = clobReader.read();
			 while (ch >= 0)
			 {
//				 if ((ch & 0xff00) > 0 ) {algorithm.update((byte)(ch >> 8 & 0xff));}
				 algorithm.update((byte)(ch & 0xff));
				 ch = clobReader.read();
			 }

			 byte messageDigest[] = algorithm.digest();
							
			 StringBuffer hexString = new StringBuffer();
			 for (int i=0;i<messageDigest.length;i++) 
			{
				  hexString.append( ((0xff & messageDigest[i]) < 16? "0": "" )+Integer.toHexString(0xFF & messageDigest[i]));
			 }
			 set(2, hexString.toString());
		} 
		catch(NoSuchAlgorithmException nsae)
		{
			throw new java.sql.SQLException("MD5 not supported: "+nsae.getMessage());
		}
		catch (Exception e) 
		{
			throw new java.sql.SQLException("Unexpected " + e.getMessage());
		}
	}



}

//CREATE FUNCTION md5( sqltext clob )
//  RETURNS char(32)
//  LANGUAGE java
//  PARAMETER STYLE java
//  NO SQL 
//  FENCED THREADSAFE
//  DETERMINISTIC 
//  RETURNS NULL ON NULL INPUT
//  NO EXTERNAL ACTION
//  EXTERNAL NAME 'myjar:udfclass.product' 
