package cokr.xit.ens.core.utils.crypto;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA256 {

    public static String encrypt(String text) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(text.getBytes());

        return bytesToHex(md.digest());
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder builder = new StringBuilder();
        for (byte b : bytes) {
            builder.append(String.format("%02x", b));
        }
        return builder.toString();
    }

//    public static String encryptV2(String raw) throws NoSuchAlgorithmException {
//
//       
////        String raw = "password1234";
//        String hex = "";
//
//       
//        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
//        byte[] bytes = new byte[16];
//        random.nextBytes(bytes);
//       
//        String salt = new String(Base64.getEncoder().encode(bytes));
//        String rawAndSalt = raw + salt;
//
//        System.out.println("raw : " + raw);
//        System.out.println("salt : " + salt);
//
//        MessageDigest md = MessageDigest.getInstance("SHA-256");
//       
//        md.update(raw.getBytes());
//        hex = String.format("%064x", new BigInteger(1, md.digest()));
//        System.out.println("raw의 해시값 : " + hex);
//
//       
//        md.update(rawAndSalt.getBytes());
//        hex = String.format("%064x", new BigInteger(1, md.digest()));
//        System.out.println("raw+salt의 해시값 : " + hex);
//        return hex;
//    }
}