package kr.co.udapsoft.common.util;

import kr.co.hsnc.common.config.WAFConfig;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;


public class Encrypt
{
    /**
     *  암호화,복호화, 단방향 암호화 MD5
     */
	public String MD5(String str){

		 String MD5 = ""; 

		 try{

			 MessageDigest md = MessageDigest.getInstance("MD5"); 

			 md.update(str.getBytes()); 

			 byte byteData[] = md.digest();

			 StringBuffer sb = new StringBuffer(); 

			 for(int i = 0 ; i < byteData.length ; i++){

				 sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));

			 }

			 MD5 = sb.toString();

			

		 }catch(NoSuchAlgorithmException e){

			 e.printStackTrace(); 

			 MD5 = null; 

		 }

		 return MD5;

	}
	
	
	public String SHA256(String str){

		 String SHA = ""; 

		 try{

			 MessageDigest sh = MessageDigest.getInstance("SHA-256"); 

			 sh.update(str.getBytes()); 

			 byte byteData[] = sh.digest();

			 StringBuffer sb = new StringBuffer(); 

			 for(int i = 0 ; i < byteData.length ; i++){

				 sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));

			 }

			 SHA = sb.toString();

			

		 }catch(NoSuchAlgorithmException e){

			 e.printStackTrace(); 

			 SHA = null; 

		 }

		 return SHA;

	}

	public String encrypt(String message) throws Exception{ 
		if(message == null){
			return null;    
		}else{
			String sKey = WAFConfig.get("waf.key.encrypt");
			SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES");
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
			byte[] encrypted = cipher.doFinal(message.getBytes());
			//System.out.println(encrypted);
			return byteArrayToHex(encrypted);
		}
	} 
	
	private static String byteArrayToHex(byte[] encrypted) {
		if(encrypted == null || encrypted.length ==0){
			return null;
		}
		StringBuffer sb = new StringBuffer(encrypted.length * 2);
		String hexNumber;
		for(int x=0; x<encrypted.length; x++){
			System.out.println( encrypted[x]);
			hexNumber = "0" + Integer.toHexString(0xff & encrypted[x]);
			//System.out.println(hexNumber);
			sb.append(hexNumber.substring(hexNumber.length() - 2));
		}
		//System.out.println(sb);
		return sb.toString();
	}
	
	public String decrypt(String encrypted) throws Exception{
		if(encrypted == null){
			return null;
		}else{
			String sKey = WAFConfig.get("waf.key.encrypt");
			
			SecretKeySpec secretKeySpec = new SecretKeySpec(sKey.getBytes(), "AES");
		    Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
			byte[] original = cipher.doFinal(hexToByteArray(encrypted));
			String originalStr = new String(original);
			return originalStr;
		}
	}
	
	private static byte[] hexToByteArray(String hex) {
		if(hex == null || hex.length() == 0){
			return null;
		}
		
		//16진수 문자열을 byte로 변환
		byte[] byteArray = new byte[hex.length() /2 ];
		for(int i=0; i<byteArray.length; i++){
			byteArray[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2*i+2), 16);
		}
		return byteArray;
	}
}
