All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.doudou.common.tool.encrypt.EncryptUtil Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.common.tool.encrypt;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @author  傻男人<[email protected]>
 * @Date: 2020/10/20 16:20
 * @Version: 1.0
 * @Description: 加密算法工具类
 */
public final class EncryptUtil {

    private static final String EMPTY_STRING = "";

    private static final String CHARSET = "UTF-8";

    private EncryptUtil(){}

    /**
     * 使用base64对字符串加密
     * @param origin
     * @return
     */
    public static String base64Encode(String origin){
        try {
            byte[] originByte = origin.getBytes(CHARSET);
            byte[] encodeByte = Base64.getEncoder().encode(originByte);
            return new String(encodeByte,CHARSET);
        }catch (UnsupportedEncodingException e){
            return EMPTY_STRING;
        }
    }

    /**
     * 使用base64对字符串进行解密
     * @param origin
     * @return
     */
    public static String base64Decode(String origin){
        try {
            byte[] originByte = origin.getBytes(CHARSET);
            byte[] decodeByte = Base64.getDecoder().decode(originByte);
            return new String(decodeByte,CHARSET);
        }catch (UnsupportedEncodingException e){
            return EMPTY_STRING;
        }
    }

    /**
     * 使用sha512加密
     * @param origin
     * @return
     */
    public static String encryptSha512(String origin){
        String shaEncode = DigestUtils.sha512Hex(origin);
        //使用base64加密
        return base64Encode(shaEncode);
    }

    /**
     * 使用sha256加密
     * @param origin
     * @return
     */
    public static String encryptSha216(String origin){
        String shaEncode = DigestUtils.sha256Hex(origin);
        //使用base64加密
        return base64Encode(shaEncode);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy