package egovframework.util;

import lombok.extern.slf4j.Slf4j;
import org.egovframe.rte.fdl.cryptography.EgovPasswordEncoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

@Slf4j
public class SHA256 {

    private static final String algorithm = "SHA-256";
    private static final String saltAlgorithm = "SHA1PRNG";


    public static String encrypt(String text) throws RuntimeException {
        EgovPasswordEncoder egovPasswordEncoder = new EgovPasswordEncoder();
        egovPasswordEncoder.setAlgorithm(algorithm);
        return egovPasswordEncoder.encryptPassword(text);
    }
//    public static String encrypt(String text) throws RuntimeException {
//        StringBuilder sb = new StringBuilder();
//        try {
//            MessageDigest md = MessageDigest.getInstance(algorithm);
//            md.update(text.getBytes());
//            byte[] data = md.digest();
//            for (byte b : data) {
//                sb.append(String.format("%02x", b & 0xFF));
//            }
//        } catch (NoSuchAlgorithmException e) {
//            throw new RuntimeException();
//        }
//        return sb.toString();
//    }

    public static String encrypt2(String planText) throws RuntimeException {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA-256");
            md.update(planText.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));
            }

            StringBuffer hexString = new StringBuffer();

            for (int i = 0; i < byteData.length; i++) {
                String hex = Integer.toHexString(0xff & byteData[i]);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
//				log.error("SHA256 encrypt2, can not encrypt planText...");
            //return planText;
            throw new RuntimeException();
        }
    }

    public static String getSalt() throws RuntimeException {
        String salt;
        try {
            SecureRandom random = SecureRandom.getInstance(saltAlgorithm);
            byte[] data = new byte[20];
            random.nextBytes(data);
            salt = new String(Base64.getEncoder().encode(data));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException();
        }
        return salt;
    }

    public static String encrypt(String text, String salt) throws RuntimeException {
        String saltText = text + salt;
        StringBuffer sb = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(saltText.getBytes());
            byte[] data = md.digest();
            for (byte b : data) {
                sb.append(String.format("%02x", b & 0xFF));
            }
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException();
        }
        return sb.toString();
    }
}
