package cn.qg.qaplatform.utils;

import sun.misc.BASE64Decoder;

import java.nio.charset.Charset;
import java.util.Base64;

/**
 * 加密工具类
 */
public class EncryptUtils {
    /**
     *
     * @param phone
     * @return
     * @throws Exception
     */
    public static String base64Encrypt(String phone) throws Exception {
        String phoneNoBase64 = encryptBASE64(phone);
        String encryptResult = "Verification " + phoneNoBase64;
        return encryptResult;
    }

    /**
     *
     * @param phone
     * @param code
     * @return
     * @throws Exception
     */
    public static String base64Encrypt(String phone, String code) throws Exception {
        String phoneNoBase64 = encryptBASE64(phone, code);
        String encryptResult = "Verification " + phoneNoBase64;
        return encryptResult;
    }

    /**
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String base64Decrypt(String key) throws Exception {
        return new String((new BASE64Decoder()).decodeBuffer(key), "UTF-8");
    }

    /**
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(String key) throws Exception {
        return Base64.getEncoder().encodeToString((key + ":0000").getBytes(Charset.forName("UTF-8")));
    }

    /**
     *
     * @param key
     * @param code
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(String key, String code) throws Exception {
        return Base64.getEncoder().encodeToString((key + ":" + "code").getBytes(Charset.forName("UTF-8")));
    }
}
