验证数据完整性——消息摘要算法

消息摘要算法包含MD(Message Digest,消息摘要算法)、SHA(Secure Hash Algorithm,安全散列算法)和MAC(Message Authentication Code,消息认证码算法)共3大系列,常用于验证数据的完整性,是数字签名算法的核心算法。

消息摘要算法又称为散列算法,其核心在于散列函数的单向性。即通过散列函数可获得对应的散列值,但不可通过该散列值反推原始信息。这是消息摘要算法的安全性的根本所在。

MD算法的使用

Sun中支持MD2和MD5算法,Bouncy Castle还支持MD4算法,Commons Codec的DigestUtils是对Sun提供的MessageDigest的一次封装,实现了MD5和SHA系列消息摘要算法的实现。

算法 摘要长度 备注
MD2 128 JAVA
MD5 同上 JAVA
MD4 ... BC

下面示例演示了MD5的处理。另外,对消息做MD5Hex处理后,得到的摘要值都是32位的十六进制字符串。

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

public abstract class MD5Coder {

    /**
     * MD5加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeMD5(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.md5(data);
    }

    /**
     * MD5加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static String encodeMD5Hex(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.md5Hex(data);
    }
}

备注:DigestUtils是一个算法工具类,在Commons-codec.jar的package org.apache.commons.codec.digest包下

commons官网

Commons-codec.jar下载或gradle配置:https://mvnrepository.com/artifact/commons-codec/commons-codec/1.14

用于校验文件的MD5值示例:

文件为 mysql-essential-5.1.38-win32.msi,存放于D盘根目录,MD5值为5a077abefee447cbb271e2aa7f6d5a47。

public class MD5Test {

    /**
     * 验证文件的MD5值
     * 
     * @throws Exception
     */
    @Test
    public void testByMessageDigest() throws Exception {
        // 文件路径
        String path = "D:\\mysql-essential-5.1.38-win32.msi";

        // 构建文件输入流
        FileInputStream fis = new FileInputStream(new File(path));

        // 初始化MessageDigest,并指定MD5算法
        DigestInputStream dis = new DigestInputStream(fis, MessageDigest
                .getInstance("MD5"));

        // 流缓冲大小
        int buf = 1024;

        // 缓冲字节数组
        byte[] buffer = new byte[buf];

        // 当读到值大于-1就继续读
        int read = dis.read(buffer, 0, buf);

        while (read > -1) {
            read = dis.read(buffer, 0, buf);
        }

        // 关闭流
        dis.close();

        // 获得MessageDigest
        MessageDigest md = dis.getMessageDigest();

        // 摘要处理
        byte[] b = md.digest();

        // 十六进制转换
        String md5hex = Hex.encodeHexString(b);

        // 验证
        assertEquals(md5hex, "5a077abefee447cbb271e2aa7f6d5a47");
    }

    /**
     * 验证文件的MD5值
     * 
     * @throws Exception
     */
    @Test
    public void testByDigestUtils() throws Exception {
        // 文件路径
        String path = "D:\\mysql-essential-5.1.38-win32.msi";

        // 构建文件输入流
        FileInputStream fis = new FileInputStream(new File(path));

        // 使用DigestUtils做MD5Hex处理
        String md5hex = DigestUtils.md5Hex(fis);

        // 关闭流
        fis.close();

        // 验证
        assertEquals(md5hex, "5a077abefee447cbb271e2aa7f6d5a47");
    }

}

SHA算法的使用

SHA算法,由美国国家安全局(NSA)设计,基于MD4算法基础演变而来,摘要的长度更长,安全性更高。

基于MD/SHA算法的消息传递

SHA家族共有SHA-1、SHA-224、SHA-256、SHA-384和SHA-512五种算法。Sun提供了SHA-1、SHA-256、SHA-384和SHA-512四种,且缺少对应的进制转换实现;Bouncy Castle提供了对SHA-224的支持;Commons Codec则是Sun的一个包装。后两者都支持多种形式的参数,支持十六进制字符串形式的摘要信息。

算法 摘要长度 备注
SHA-1 160 JAVA
SHA-256 256 JAVA
SHA-384 384 JAVA
SHA-512 512 JAVA
SHA-224 224 BC
import java.security.MessageDigest;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import org.apache.commons.codec.digest.DigestUtils;

public abstract class SHACoder {

    /**
     * SHA加密
     * 
     * @param data 待加密数据
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeSHA(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.sha(data);
    }

    /**
     * SHAHex加密
     * 
     * @param data 待加密数据
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeSHAHex(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.shaHex(data);
    }

    /**
     * SHA-224加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeSHA224(byte[] data) throws Exception {
        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("SHA-224");

        // 执行消息摘要
        return md.digest(data);
    }

    /**
     * SHA-224加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static String encodeSHA224Hex(byte[] data) throws Exception {

        // 执行消息摘要
        byte[] b = encodeSHA224(data);

        // 做十六进制编码处理
        return new String(Hex.encode(b));

    }

    /**
     * SHA256加密
     * 
     * @param data 待加密数据
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeSHA256(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.sha256(data);
    }

    /**
     * SHA256Hex加密
     * 
     * @param data 待加密数据
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeSHA256Hex(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.sha256Hex(data);
    }

    /**
     * SHA384加密
     * 
     * @param data 待加密数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static byte[] encodeSHA384(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.sha384(data);
    }

    /**
     * SHA384Hex加密
     * 
     * @param data 待加密数据
     * @return String 消息摘要 
     * @throws Exception
     */
    public static String encodeSHA384Hex(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.sha384Hex(data);
    }

    /**
     * SHA512Hex加密
     * 
     * @param data 待加密数据
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeSHA512(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.sha512(data);
    }

    /**
     * SHA512Hex加密
     * 
     * @param data 待加密数据
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeSHA512Hex(String data) throws Exception {

        // 执行消息摘要
        return DigestUtils.sha512Hex(data);
    }

}

MAC算法的处理

MAC算法结合了MD5和SHA算法的优势,并加入密钥的支持,是一种更为安全的消息摘要算法。因此,也常把MAC称为HMAC(keyed-Hash Message Authentication Code)。

基于MAC算法的消息传递模型

MAC算法主要集合了MD和SHA两大系列消息摘要算法。MD系列算法有HmacMD2、HmacMD4和HmacMD5三种;SHA系列算法有HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384和HmacSHA512五种算算法。

Sun提供了基本算法支持,如HmacMD5、HmacSHA1、HmacSHA256、HmacSHA384和HmacSHA512五种;而Bouncy Castle则增加了对HmacMD2、HmacMD4和HmacSHA224三种算法的支持,同时支持十六进制编码。

算法 摘要长度 备注
HmacMD5 128 JAVA6实现
HmacSHA1 160 JAVA6实现
HmacSHA256 256 JAVA6实现
HmacSHA384 384 JAVA6实现
HmacSHA512 512 JAVA6实现
HmacMD2 128 BouncyCastle实现
HmacMD4 128 BouncyCastle实现
HmacSHA224 224 BouncyCastle实现
import java.security.Security;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

public abstract class MACCoder {

    /**
     * 初始化HmacMD5密钥
     * 
     * @return
     * @throws Exception
     */
    public static byte[] initHmacMD5Key() throws Exception {

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacMD5加密
     * 
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeHmacMD5(byte[] data, byte[] key)
            throws Exception {

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacMD5");

        // 实例化Mac "SslMacMD5"
        Mac mac = Mac.getInstance("SslMacMD5");//secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * 初始化HmacSHA1密钥
     * 
     * @return
     * @throws Exception
     */
    public static byte[] initHmacSHAKey() throws Exception {

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HMacTiger");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacSHA1加密
     * 
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeHmacSHA(byte[] data, byte[] key)
            throws Exception {

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HMacTiger");
 
        // 实例化Mac SslMacMD5
        Mac mac = Mac.getInstance("SslMacMD5");//secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }
    
//  // 根据所安装的 JCE 仲裁策略文件,返回指定转换的最大密钥长度。
//  public final static int getMaxAllowedKeyLength(String transformation) 

    /**
     * 初始化HmacSHA256密钥
     * 
     * @return
     * @throws Exception
     */
    public static byte[] initHmacSHA256Key() throws Exception {

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA256");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacSHA256加密
     * 
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeHmacSHA256(byte[] data, byte[] key)
            throws Exception {

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");

        // 实例化Mac
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * 初始化HmacSHA384密钥
     * 
     * @return
     * @throws Exception
     */
    public static byte[] initHmacSHA384Key() throws Exception {

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA384");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacSHA384加密
     * 
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeHmacSHA384(byte[] data, byte[] key)
            throws Exception {

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");

        // 实例化Mac
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * 初始化HmacSHA512密钥
     * 
     * @return
     * @throws Exception
     */
    public static byte[] initHmacSHA512Key() throws Exception {

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacSHA512加密
     * 
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeHmacSHA512(byte[] data, byte[] key)
            throws Exception {

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");

        // 实例化Mac
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * 初始化HmacMD2密钥
     * 
     * @return byte[] 密钥
     * @throws Exception
     */
    public static byte[] initHmacMD2Key() throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD2");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacMD2消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param byte[] 密钥
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeHmacMD2(byte[] data, byte[] key)
            throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacMD2");

        // 实例化Mac
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * HmacMD2Hex消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param String
     *            密钥
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static String encodeHmacMD2Hex(byte[] data, byte[] key)
            throws Exception {

        // 执行消息摘要
        byte[] b = encodeHmacMD2(data, key);

        // 做十六进制转换
        return new String(Hex.encode(b));
    }

    /**
     * 初始化HmacMD4密钥
     * 
     * @return byte[] 密钥
     * @throws Exception
     */
    public static byte[] initHmacMD4Key() throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD4");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacMD4消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param byte[] 密钥
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeHmacMD4(byte[] data, byte[] key)
            throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacMD4");

        // 实例化Mac
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * HmacMD4Hex消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param byte[] 密钥
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeHmacMD4Hex(byte[] data, byte[] key)
            throws Exception {

        // 执行消息摘要
        byte[] b = encodeHmacMD4(data, key);

        // 做十六进制转换
        return new String(Hex.encode(b));
    }

    /**
     * 初始化HmacSHA224密钥
     * 
     * @return byte[] 密钥
     * @throws Exception
     */
    public static byte[] initHmacSHA224Key() throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA224");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacSHA224消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param byte[] 密钥
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeHmacSHA224(byte[] data, byte[] key)
            throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacSHA224");

        // 实例化Mac
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * HmacSHA224Hex消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param byte[] 密钥
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeHmacSHA224Hex(byte[] data, byte[] key)
            throws Exception {

        // 执行消息摘要
        byte[] b = encodeHmacSHA224(data, key);

        // 做十六进制转换
        return new String(Hex.encode(b));
    }
}

其他消息摘要算法

RipeMD算法,是在MD4、MD5缺陷分析的基础上提出的。共有4个标准,包括RipeMD128、RipeMD160、RipeMD256和RipeMD320。RipeMD和MAC算法融合产生了HmacRipeMD128和HmacRipeMD160。

算法 摘要长度 备注
RipeMD128 128 BouncyCastle实现
RipeMD160 160 BouncyCastle实现
RipeMD256 256 BouncyCastle实现
RipeMD320 320 BouncyCastle实现
HmacRipeMD128 128 BouncyCastle实现
HmacRipeMD160 160 BouncyCastle实现
import java.security.Security;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

public abstract class RipeMDCoder {

    /**
     * RipeMD128消息摘要
     * 
     * @param data 待做消息摘要处理的数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static byte[] encodeRipeMD128(byte[] data) throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("RipeMD128");

        // 执行消息摘要
        return md.digest(data);
    }

    /**
     * RipeMD128Hex消息摘要
     * 
     * @param data 待做消息摘要处理的数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static String encodeRipeMD128Hex(byte[] data) throws Exception {

        // 执行消息摘要
        byte[] b = encodeRipeMD128(data);

        // 做十六进制编码处理
        return new String(Hex.encode(b));
    }

    /**
     * RipeMD160消息摘要
     * 
     * @param data 待做消息摘要处理的数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static byte[] encodeRipeMD160(byte[] data) throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("RipeMD160");

        // 执行消息摘要
        return md.digest(data);
    }

    /**
     * RipeMD160Hex消息摘要
     * 
     * @param data 待做消息摘要处理的数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static String encodeRipeMD160Hex(byte[] data) throws Exception {

        // 执行消息摘要
        byte[] b = encodeRipeMD160(data);

        // 做十六进制编码处理
        return new String(Hex.encode(b));
    }

    /**
     * RipeMD256消息摘要
     * 
     * @param data 待做消息摘要处理的数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static byte[] encodeRipeMD256(byte[] data) throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("RipeMD256");

        // 执行消息摘要
        return md.digest(data);
    }

    /**
     * RipeMD256Hex消息摘要
     * 
     * @param data 待做消息摘要处理的数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static String encodeRipeMD256Hex(byte[] data) throws Exception {

        // 执行消息摘要
        byte[] b = encodeRipeMD256(data);

        // 做十六进制编码处理
        return new String(Hex.encode(b));
    }

    /**
     * RipeMD320消息摘要
     * 
     * @param data 待做消息摘要处理的数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static byte[] encodeRipeMD320(byte[] data) throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("RipeMD320");

        // 执行消息摘要
        return md.digest(data);
    }

    /**
     * RipeMD320Hex消息摘要
     * 
     * @param data 待做消息摘要处理的数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static String encodeRipeMD320Hex(byte[] data) throws Exception {

        // 执行消息摘要
        byte[] b = encodeRipeMD320(data);

        // 做十六进制编码处理
        return new String(Hex.encode(b));
    }

    /**
     * 初始化HmacRipeMD128密钥
     * 
     * @return byte[] 密钥
     * @throws Exception
     */
    public static byte[] initHmacRipeMD128Key() throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacRipeMD128");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacRipeMD128消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param byte[] 密钥
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeHmacRipeMD128(byte[] data, byte[] key)
            throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacRipeMD128");

        // 实例化Mac
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * HmacRipeMD128Hex消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param String
     *            密钥
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static String encodeHmacRipeMD128Hex(byte[] data, byte[] key)
            throws Exception {

        // 执行消息摘要
        byte[] b = encodeHmacRipeMD128(data, key);

        // 做十六进制转换
        return new String(Hex.encode(b));
    }

    /**
     * 初始化HmacRipeMD160密钥
     * 
     * @return byte[] 密钥
     * @throws Exception
     */
    public static byte[] initHmacRipeMD160Key() throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化KeyGenerator
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacRipeMD160");

        // 产生秘密密钥
        SecretKey secretKey = keyGenerator.generateKey();

        // 获得密钥
        return secretKey.getEncoded();
    }

    /**
     * HmacRipeMD160消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param byte[] 密钥
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeHmacRipeMD160(byte[] data, byte[] key)
            throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 还原密钥
        SecretKey secretKey = new SecretKeySpec(key, "HmacRipeMD160");

        // 实例化Mac
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());

        // 初始化Mac
        mac.init(secretKey);

        // 执行消息摘要
        return mac.doFinal(data);
    }

    /**
     * HmacRipeMD160Hex消息摘要
     * 
     * @param data
     *            待做消息摘要处理的数据
     * @param byte[] 密钥
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeHmacRipeMD160Hex(byte[] data, byte[] key)
            throws Exception {

        // 执行消息摘要
        byte[] b = encodeHmacRipeMD160(data, key);

        // 做十六进制转换
        return new String(Hex.encode(b));
    }

}

Tiger算法,号称最快的HASH算法,专门为64为机器做了优化,其消息摘要长度为192位。

Whirlpool算法,号称最安全的摘要算法,长度512位。

GOST3411算法,长度256位,作者不详。

import java.security.MessageDigest;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

public abstract class MDCoder {

    /**
     * Tiger加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeTiger(byte[] data) throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("Tiger");

        // 执行消息摘要
        return md.digest(data);
    }

    /**
     * TigerHex加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static String encodeTigerHex(byte[] data) throws Exception {

        // 执行消息摘要
        byte[] b = encodeTiger(data);

        // 做十六进制编码处理
        return new String(Hex.encode(b));
    }

    /**
     * Whirlpool加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeWhirlpool(byte[] data) throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("Whirlpool");

        // 执行消息摘要
        return md.digest(data);
    }

    /**
     * WhirlpoolHex加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static String encodeWhirlpoolHex(byte[] data) throws Exception {

        // 执行消息摘要
        byte[] b = encodeWhirlpool(data);

        // 做十六进制编码处理
        return new String(Hex.encode(b));
    }

    /**
     * GOST3411加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeGOST3411(byte[] data) throws Exception {

        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());

        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("GOST3411");

        // 执行消息摘要
        return md.digest(data);
    }

    /**
     * GOST3411Hex加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static String encodeGOST3411Hex(byte[] data) throws Exception {

        // 执行消息摘要
        byte[] b = encodeGOST3411(data);

        // 做十六进制编码处理
        return new String(Hex.encode(b));
    }
}

CRC(Cyclic Redundancy Check,循环冗余校验)算法,并不属于加密算法。 CRC-32是各种压缩算法中最为常见的数据完整性校验算法,而它的变种Adler-32普遍应用于zlib压缩算法中的数据完整性校验。

压缩/解压缩数据传递模型

发布了205 篇原创文章 · 获赞 217 · 访问量 235万+

猜你喜欢

转载自blog.csdn.net/heng615975867/article/details/105345918