package org.apache.drill.contrib.function;

import java.security.*;

import javax.inject.Inject;

import org.apache.drill.exec.expr.DrillSimpleFunc;
import org.apache.drill.exec.expr.annotations.FunctionTemplate;
import org.apache.drill.exec.expr.annotations.Output;
import org.apache.drill.exec.expr.annotations.Param;
import org.apache.drill.exec.expr.annotations.Workspace;
import org.apache.drill.exec.expr.holders.NullableVarCharHolder;
import org.apache.drill.exec.expr.holders.VarCharHolder;

import io.netty.buffer.DrillBuf;

@FunctionTemplate(
        name = "sha1",
        scope = FunctionTemplate.FunctionScope.SIMPLE,
        nulls = FunctionTemplate.NullHandling.NULL_IF_NULL
)
public class SHA1Func implements DrillSimpleFunc  {

	@Param
    NullableVarCharHolder input;

    @Output
    VarCharHolder out;

    @Inject
    DrillBuf buffer;
    
    @Workspace java.security.MessageDigest md;
    
	public void setup() {
		try {
			md = MessageDigest.getInstance("SHA1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void eval() {
        String stringValue = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(input.start, input.end, input.buffer);

        String outputValue = "";

		byte[] result = md.digest(stringValue.getBytes());
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < result.length; i++) {
			sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
		}
		
		outputValue = sb.toString();

		
        // put the output value in the out buffer
        out.buffer = buffer;
        out.start = 0;
        out.end = outputValue.getBytes().length;
        buffer.setBytes(0, outputValue.getBytes());
	}

//	private static final String encSha1(String input) {
//		try {
//			MessageDigest mDigest = MessageDigest.getInstance("SHA1");
//			byte[] result = mDigest.digest(input.getBytes());
//			StringBuffer sb = new StringBuffer();
//			for (int i = 0; i < result.length; i++) {
//				sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
//			}
//			
//			return sb.toString();
//	    } catch (NoSuchAlgorithmException e) {
//	        e.printStackTrace();
//	    }
//	    return "";
//    }
	
//     public static void main(String args[]) {
//    	 String stringValue = "abcde";
//         System.out.println(encSha1(stringValue));
//     }
}

@@ -1490,19 +1490,16 @@
 ing();%0A%0A
-%09%09%0A
         
@@ -1699,665 +1699,7 @@
 ;%0A%09%7D
-%0A%0A//%09private static final String encSha1(String input) %7B%0A//%09%09try %7B%0A//%09%09%09MessageDigest mDigest = MessageDigest.getInstance(%22SHA1%22);%0A//%09%09%09byte%5B%5D result = mDigest.digest(input.getBytes());%0A//%09%09%09StringBuffer sb = new StringBuffer();%0A//%09%09%09for (int i = 0; i %3C result.length; i++) %7B%0A//%09%09%09%09sb.append(Integer.toString((result%5Bi%5D & 0xff) + 0x100, 16).substring(1));%0A//%09%09%09%7D%0A//%09%09%09%0A//%09%09%09return sb.toString();%0A//%09    %7D catch (NoSuchAlgorithmException e) %7B%0A//%09        e.printStackTrace();%0A//%09    %7D%0A//%09    return %22%22;%0A//    %7D%0A%09%0A//     public static void main(String args%5B%5D) %7B%0A//    %09 String stringValue = %22abcde%22;%0A//         System.out.println(encSha1(stringValue));%0A//     %7D
 %0A%7D%0A

주석 제거