消息摘要算法

消息摘要算法可以保证消息的完整性

一 、MD算法

MD算法有MD2、MD3、MD4、MD5,MD5是前面算法改进而来,如果需要使用其他算法,下面的MD5都可以转换为响应的算法
1. 使用sun提供的MD5消息摘要API作如下
  当然除了如下所示方法MessageDigest也提供对DigestInputStream和DigestOutPutStream做消息摘要。

    import java.security.MessageDigest;
    
    public class MD5Test01 {
        /**
        *测试
        */
        public static void main(String[] args) throws Exception {
            String str ="hello world";
            byte[] bytes1 = encodeMD5Digest(str.getBytes());
            System.out.println("digest : "+new String(bytes1,"UTF-8"));
    
            byte[] bytes2 = encodeMD5Update(str.getBytes());
            System.out.println("update : "+new String(bytes2,"UTF-8"));
        }
    
        /**
         * MD5消息摘要算法,用digest摘要
         *
         * @param data
         * @return
         * @throws Exception
         */
        public static byte[] encodeMD5Digest(byte[] data) throws Exception {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            return messageDigest.digest(data);
        }
    
        /**
         * 使用update做消息散列
         * @param data
         * @return
         * @throws Exception
         */
        public static byte[] encodeMD5Update(byte[] data) throws Exception {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(data);
            return messageDigest.digest();
        }
    }

2. 使用Commons Codec 实现MD5算法
  这个需要自己导入Commons Codec包,这个包中对于Java提供的原生MessageDigest做了封装,并提供更多的方法如toHex转换为16进制的数据。

   import org.apache.commons.codec.digest.DigestUtils;
    
    public class MD5Test02 {
    
        public static void main(String[] args) {
    
            String str = "hello world";
            System.out.println("encode: " + new String(encodeMD5(str)));
            System.out.println("encode to Hex : " + encodeMD5Hex(str));
        }
    
        public static byte[] encodeMD5(String data) {
            return DigestUtils.md5(data);
        }
    
        public static String encodeMD5Hex(String data) {
            return DigestUtils.md5Hex(data);
        }
    }

二、SHA算法

  SHA算法有SHA-1、SHA-224、SHA-256、SHA-384、SHA-512,后面四种也称为SHA-2; SHA-1的摘要长度是160,其他的摘要长度都是后面所带的数字。 SHA-1可以直接使用SHA简写写法。

1. 使用sun提供的SHA消息摘要API作如下

    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    public class SHA {
    
        public static void main(String[] args) throws NoSuchAlgorithmException {
    
            String str ="hello world";
            byte[] bytes1 = encodeSHADigest(str.getBytes());
            System.out.println("digest: "+ new String(bytes1));
            byte[] bytes2 = encodeSHAUpdate(str.getBytes());
            System.out.println("update: "+ new String(bytes2));
        }
        public static byte[] encodeSHADigest(byte[] data) throws NoSuchAlgorithmException {
            MessageDigest sha = MessageDigest.getInstance("SHA");
            return sha.digest(data);
        }
        public static byte[] encodeSHAUpdate(byte[] data) throws NoSuchAlgorithmException {
            MessageDigest sha = MessageDigest.getInstance("SHA");
            sha.update(data);
            return sha.digest();
        }
    }

2. 使用Commons Codec 实现SHA算法

    import org.apache.commons.codec.digest.DigestUtils;
    
    public class SHATest {
    
        public static void main(String[] args) {
            String str = "hello world";
            System.out.println("encode:" + new String(encodeSHA(str)));
            System.out.println("encode to Hex:" + encodeSHAHex(str));
        }
        public static byte[] encodeSHA(String data){
            return DigestUtils.sha(data);
        }
    
        public static String encodeSHAHex(String data){
            return DigestUtils.shaHex(data);
        }
    }

三、MAC 算法

  MAC算法 结合了MD5和SHA算法的优势,并加入了密匙的支持,是一种更为安全的消息摘要算法。
  MD系列算法有HmacMD2、HmacMD4和HmacMD5三种;SHA系列算法有HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384和HmacSHA512算法。

消息传递模型:
在这里插入图片描述

 `Mac算法是带有密匙的消息摘要算法,所以实现有两步:
  1)构建秘钥 
  2)执行消息摘要`

sun提供的MAC消息摘要API作如下

    import javax.crypto.KeyGenerator;
    import javax.crypto.Mac;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.NoSuchAlgorithmException;
    
    public class MACTest01 {
    
        public static void main(String[] args) throws Exception {
    
            String str = "hello vison";
            byte[] key = initHmacSHAKey();
            byte[] bytes = encodeHmacSHA(str.getBytes(), key);
            System.out.println("encode: "+ new String (bytes));
        }
    
        /**
         * 初始化HmacSHA1密匙
         * @return
         * @throws NoSuchAlgorithmException
         */
        public static byte[] initHmacSHAKey() throws NoSuchAlgorithmException {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA1");
            //产生密匙
            SecretKey secretKey = keyGenerator.generateKey();
            //获取密匙
            return  secretKey.getEncoded();
        }
    
        public static byte[] encodeHmacSHA(byte[] data,byte[] key) throws Exception {
            //还原密匙
            SecretKey secretKey = new SecretKeySpec(key,"HmacSHA1");
            //实例化Mac
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            //初始化Mac
            mac.init(secretKey);
            //执行消息摘要
            return mac.doFinal();
        }
    }

四、循环冗余校验算法-CRC算法

  CRC算法,奇偶校验码 ,循环冗余校验算法都是同一套东西,CRC(Cyclic Redundancy Check)是可以根据数据产生简短固定位数的一种散列函数,主要是用来检测或者校验数据传输/保存后出现的错误。把生成的散列值在传输或储存之前计算出来并且附加到数据后面。

    public class CRC32Test {
    
        public static void main(String[] args) {
    
            String str = "hello vison";
            CRC32 crc32 = new CRC32();
            crc32.update(str.getBytes());
    
            String hex = Long.toHexString(crc32.getValue());
            System.out.println("to hex: "+ hex);
        }
    }
    ```
参考:
   《Java加密与解密的艺术》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值