程序包 com.ajaxjs.util

类 MessageDigestHelper


  • public class MessageDigestHelper
    extends Object
    字符串摘要(哈希)工具类
    • 构造器详细资料

      • MessageDigestHelper

        public MessageDigestHelper()
    • 方法详细资料

      • getResult

        public String getResult()
        返回摘要的结果,可选择 Base64 编码或者转换未 Hex 字符串
        返回:
        摘要的结果
      • getMd5

        public static String getMd5​(String str)
        生成字符串的 MD5 哈希值,等价于 DigestUtils.md5DigestAsHex()
        参数:
        str - 输入的字符串
        返回:
        字符串的 MD5 哈希值,返回32位小写的字符串
      • getSHA1

        public static String getSHA1​(String str)
        生成字符串的 SHA1 哈希值
        参数:
        str - 输入的字符串
        返回:
        字符串的 SHA1 哈希值
      • getSHA256

        public static String getSHA256​(String str)
        生成字符串的 SHA256 哈希值
        参数:
        str - 输入的字符串
        返回:
        字符串的 SHA256 哈希值
      • getMd5AsBase64

        public static String getMd5AsBase64​(String str)
        生成字符串的 MD5 哈希值,Base64 编码
        参数:
        str - 输入的字符串
        返回:
        字符串的 MD5 哈希值,Base64 编码
      • getMac

        public static byte[] getMac​(String algorithmName,
                                    String key,
                                    String data)
        获取指定算法的 MAC 值(可设密钥)
        参数:
        algorithmName - 算法名称
        key - 用于生成 MAC 值的密钥
        data - 要进行 MAC 计算的数据
        返回:
        生成的 MAC 值
      • getMd5

        public static String getMd5​(String key,
                                    String str)
        获取字符串的 MD5 值
        参数:
        key - 生成 MD5 值的密钥
        str - 要生成 MD5 值的字符串
        返回:
        生成的 MD5 值
      • getMd5AsBase64

        public static String getMd5AsBase64​(String key,
                                            String str)
        获取字符串的 MD5 值并转换为 Base64 编码
        参数:
        key - 生成 MD5 的密钥
        str - 需要生成 MD5 的字符串
        返回:
        MD5 值的 Base64 编码
      • getHmacSHA1AsBase64

        public static String getHmacSHA1AsBase64​(String key,
                                                 String str)
      • getHmacSHA256AsBase64

        public static String getHmacSHA256AsBase64​(String key,
                                                   String str)
      • calcFileMD5

        public static String calcFileMD5​(File file,
                                         byte[] bytes)
        计算文件 MD5
        参数:
        file - 文件对象。该参数与 bytes 二选一
        bytes - 文件字节。该参数与 file 二选一
        返回:
        返回文件的 md5 值,如果计算过程中任务的状态变为取消或暂停,返回 null, 如果有其他异常,返回空字符串
      • md5

        public static String md5​(String str)
        计算一个字符串的 MD5 值
        参数:
        str - 待计算 MD5 的字符串
        返回:
        计算结果