package com.eactive.eai.transformer.function.crypto;

import java.util.Stack;

import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;


public class SamsungPayRSADecode extends PostfixMathCommand {
    public SamsungPayRSADecode() {
        numberOfParameters = 1;
    }

    public void run(Stack inStack) throws ParseException {
 		String localKey = "88888888";
    	String pubKeyFile="/fsapp/eai/MagicPKI/key/public.key";	      // 공개키 저장파일명
		String priEncKeyFile="/fsapp/eai/MagicPKI/key/private.key";   // 개인키 저장파일명
		
    	checkStack(inStack);

        Object param = inStack.pop();

        byte[] value = null;

        if (param instanceof String) {
            value = ((String) param).getBytes();
        } else if (param instanceof byte[]) {
            value = (byte[]) param;
        } else if (param instanceof Double) {
        	if (((Double) param).doubleValue() == Math.round(((Double) param).doubleValue())) {
        		value = new Long(Math.round(((Double) param).doubleValue())).toString().getBytes();
        	} else {
                value =((Number) param).toString().getBytes();
        	}
        } else if (param instanceof Number) {
            value = ((Number) param).toString().getBytes();
        } else if (param == null) {
//        	System.out.println("RSA : Data is NULL");
            inStack.push(new byte[0]);
            return;
        } else {
            throw new ParseException("Function의 파라미터 인수가 byte[], String이 아닙니다. : " + param);
        }

        try {
//        	DSToolkit.init("/fsapp/eai/MagicPKI/license");
//    		PrivateKey priKey = null;
//            PublicKey  pubKey = null;
//    		pubKey = new PublicKey( Disk.read(pubKeyFile) );
//			priKey = Disk.readPriKey( priEncKeyFile, localKey );
//			
//			Base64 base64 = new Base64();
//			byte[] decValue = null;
//			
//			Cipher rsa = Cipher.getInstance("RSA");
//			rsa.init(Cipher.DECRYPT_MODE, priKey);
//			decValue = base64.decode(new String(value));
//			byte[] res = rsa.doFinal(decValue);
//			
//			inStack.push(res);
//	        			
			
			
        } catch (Exception e) {
            //throw new ExecutionException(e.getMessage(), parameters, e);
            throw new ParseException(e.getMessage(), e);
        }    	
    }
    
    
}