package com.qunzhixinxi.hnqz.admin.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

/**
 * {@code AESUtils} AES加密工具类
 *
 * @author Hengchen.Sun
 * @version 1.0.1
 * @date 2021-04-06
 * @date 2021-06-03 modify 更新获取linux秘钥私有方法
 * @since ver.1.0.0
 */
@Slf4j
public final class AESUtils {

	private AESUtils() {
	}

	private static final String KEY_ALGORITHM = "AES";
	private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法
	private static final String CHARSET = "UTF-8";

	/**
	 * AES 加密操作
	 *
	 * @param content  待加密内容
	 * @param password 加密密码
	 * @return 返回Base64转码后的加密数据
	 */
	public static String encrypt(String content, String password) throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchProviderException {
		Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
		byte[] byteContent = content.getBytes(CHARSET);
		cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));// 初始化为加密模式的密码器
		byte[] result = cipher.doFinal(byteContent);// 加密
		return Base64Utils.encodeToString(result);//通过Base64转码返回
	}

	/**
	 * AES 解密操作
	 *
	 * @param content
	 * @param password
	 * @return
	 */
	public static String decrypt(String content, String password) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, NoSuchProviderException {
		//实例化
		Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
		//使用密钥初始化，设置为解密模式
		cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
		//执行操作
		byte[] result = cipher.doFinal(Base64Utils.decodeFromString(content));
		return new String(result, CHARSET);
	}

	/**
	 * 生成加密秘钥
	 * @date 2021-06-03 modify 更新获取linux秘钥私有方法
	 * @return
	 */
	private static SecretKeySpec getSecretKey(String password) throws NoSuchAlgorithmException, NoSuchProviderException {
		String osName = OsEnvUtils.getOSName();
		//返回生成指定算法密钥生成器的 KeyGenerator 对象
		SecureRandom random;
		if (osName.contains("linux") || osName.contains("mac")){
			random = SecureRandom.getInstance("SHA1PRNG","SUN");
			random.setSeed(password.getBytes());
		} else {
			// windows
			random = new SecureRandom(password.getBytes());
		}
		KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
		//AES 要求密钥长度为 128
		kg.init(128,random);
		return new SecretKeySpec(kg.generateKey().getEncoded(), KEY_ALGORITHM);// 转换为AES专用密钥
	}
}
