package com.chushang.common.core.web;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * by zhaowenyuan create 2021/12/30 11:42
 */
@Slf4j
@UtilityClass
public class TokenUtils {
    private final String TOKEN_SIGN = "sane_cloud_token";

    public String decryptStr(String sanyiToken){
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, TOKEN_SIGN.getBytes(StandardCharsets.UTF_8));
        try {
            return aes.decryptStr(sanyiToken);
        }catch (Exception ignored){
        }
        return null;
    }

    public static String decryptStr(String sanyiToken, String signType) {
        // SymmetricAlgorithm.valueof() 必定不为空, 并且不会异常
        try {
            SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.valueOf(signType), TOKEN_SIGN.getBytes(StandardCharsets.UTF_8));
            return aes.decryptStr(sanyiToken);
        }catch (Exception ignored){
        }
        return null;
    }
}
