package com.sckw.core.utils;

import com.alibaba.fastjson2.JSON;

import java.security.MessageDigest;
import java.util.UUID;

/**
 * 密码工具类
 *
 * @author Louis
 * @date Sep 1, 2018
 */
public class PasswordUtils {
    public static final int HASH_INTERATIONS = 1024;
    public static final int SALT_SIZE = 8;
    public static final int SUB_LENGTH = 16;

    /**
     * 匹配密码
     *
     * @param salt    盐
     * @param rawPass 明文
     * @param encPass 密文
     * @return
     */
    public static boolean matches(String salt, String rawPass, String encPass) {
        return new EncryptionUtil(salt).matches(encPass, rawPass);
    }

    /**
     * 明文密码加密
     *
     * @param rawPass 明文
     * @param salt
     * @return
     */
    public static String encode(String rawPass, String salt) {
        return new EncryptionUtil(salt).encode(rawPass);
    }

    /**
     * 获取加密盐
     *
     * @return
     */
    public static String getSalt() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 20);
    }

    /**
     * MD5加密
     *
     * @param inStr 明文
     * @return 32位密文
     */
    public static String md5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] byteArray = inStr.getBytes("UTF-8");
            byte[] md5Bytes = md5.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
    }

    /**
     * md5密码校验
     *
     * @param rawPass
     * @param encPass
     * @return
     */
    public static boolean matchesMD5(String rawPass, String encPass) {
        if (md5(rawPass).equals(encPass)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 生成安全的密码，生成随机的16位salt并经过1024次 sha-1 hash
     */
    public static String entryptPassword(String plainPassword) {
        byte[] salt = Digests.generateSalt(SALT_SIZE);
        byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt, HASH_INTERATIONS);
        return Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword);
    }

    /**
     * 截取密文密码生成盐
     */
    public static String getSaltSubPwd(String password) {
        return password.substring(0, SUB_LENGTH);
    }

    /**
     * 验证密码
     *
     * @param plainPassword 明文密码
     * @param password      密文密码
     * @return 验证成功返回true
     */
    public static boolean validatePassword(String plainPassword, String password) {
        byte[] salt = Encodes.decodeHex(password.substring(0, SUB_LENGTH));
        byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt, HASH_INTERATIONS);
        return password.equals(Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword));
    }

    public static void main(String[] args) {

        String password = PasswordUtils.entryptPassword(PasswordUtils.md5("18581845668"));
        String md5 = PasswordUtils.md5("123456");
        System.out.println(password);
        System.out.println(md5);
        System.out.println(validatePassword(md5, password));
//        System.out.println(PasswordUtils.md5("czh"));
//        System.out.println(PasswordUtils.entryptPassword(PasswordUtils.md5("czh")));
        System.out.println(validatePassword(md5, "86e07d48c04c8a4bd9fe9dc819c608c43efda576b215995e9f138809"));


        byte[] salt = Digests.generateSalt(SALT_SIZE);
        byte[] hashPassword = Digests.sha1(md5.getBytes(), salt, HASH_INTERATIONS);
        System.out.println(Encodes.encodeHex(salt));
        System.out.println(Encodes.encodeHex(hashPassword));
    }
}
