SHA 加密算法工具类

 

 

SHAUtils.java

package javax.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA 加密算法
 */
public class SHAUtils {

    /**
     * 加密成字节数组
     */
    public static byte[] encrypt(String s) {
        try {
            MessageDigest sha = MessageDigest.getInstance("SHA");
            return sha.digest(s.getBytes(StandardCharsets.UTF_8));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 加密成大写字符串
     */
    public static String encryptToUpperString(String s) {
        return encryptToString(s, "%02X");
    }

    /**
     * 加密成小写字符串
     */
    public static String encryptToLowerString(String s) {
        return encryptToString(s, "%02x");
    }

    /**
     * 加密成字符串
     */
    private static String encryptToString(String s, String format) {
        byte[] digest = encrypt(s);
        StringBuilder build = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            build.append(String.format(format, digest[i]));
        }

        return build.toString();
    }

}

 

 

 

.

posted @ 2020-06-16 16:54  诚信天下  阅读(431)  评论(0编辑  收藏  举报