package io.hmit.modules.security.password;

import sun.misc.BASE64Decoder;

import java.io.IOException;
import java.security.MessageDigest;

/**
 * 密码工具类
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0
 */
public class PasswordUtils {

    private static PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    /**
     * 加密
     *
     * @param str 字符串
     * @return 返回加密字符串
     */
    public static String encode(String str) {
        return passwordEncoder.encode(str);
    }


    /**
     * 比较密码是否相等
     *
     * @param str      明文密码
     * @param password 加密后密码
     * @return true：成功    false：失败
     */
    public static boolean matches(String str, String password) {
        return passwordEncoder.matches(str, password);
    }

    /**
     * Base64 解密
     * @param str    明文密码
     * @return 返回BASE64解密后的密码
     */
    public static String JM(String str) {
        if (str == null) return null;

        String decoderStr = "";

        try {
            BASE64Decoder decoder = new BASE64Decoder();

            byte[] b = decoder.decodeBuffer(str);

            decoderStr = new String(b);

        } catch (IOException e) {
            e.printStackTrace();

            return null;

        }
        return decoderStr;
}

    public static void main(String[] args) {
        String str = "admin";
        String password = encode(str);

        System.out.println(JM("bmloYW8="));
        System.out.println(matches(str, password));
    }

}
