为了账号安全,请及时绑定邮箱和手机立即绑定

消息摘要算法的java代码

标签:
Java

消息摘要算法
1.MD5

package com.security3;

import java.security.MessageDigest;
import java.security.Security;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class LoveMD {
    private static String class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="imooc security md";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        jdkMD5();
        bcMD4();
        bcMD5();
        bcMD42();
        ccMD5();
        ccMD2();

    }
    public static void jdkMD5(){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] md5Bytes=md.digest(src.getBytes());
            System.out.println("JDK MD5:"+Hex.encodeHexString(md5Bytes));
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static void bcMD4(){
        Digest digest=new MD4Digest();
        digest.update(src.getBytes(), 0,src.getBytes().length);
        byte[] md4Bytes=new byte[digest.getDigestSize()];
        digest.doFinal(md4Bytes, 0);
        System.out.println("BC MD4:"+org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
    }
    public static void bcMD5(){
        Digest digest=new MD5Digest();
        digest.update(src.getBytes(), 0,src.getBytes().length);
        byte[] md5Bytes=new byte[digest.getDigestSize()];
        digest.doFinal(md5Bytes, 0);
        System.out.println("BC MD5:"+org.bouncycastle.util.encoders.Hex.toHexString(md5Bytes));
    }
    public static void bcMD42(){
        try {
            Security.addProvider(new BouncyCastleProvider());
            MessageDigest md = MessageDigest.getInstance("MD4");
            byte[] md4Bytes = md.digest(src.getBytes());
            System.out.println("BC MD42:" + org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static void ccMD5(){
        System.out.println("CC MD5:"+DigestUtils.md5Hex(src.getBytes()));
    }
    public static void ccMD2(){
        System.out.println("CC MD2:"+DigestUtils.md2Hex(src.getBytes()));
    }
}

2.SHA

package com.security3;

import java.security.MessageDigest;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;

public class LoveSHA {
    private static String class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="imooc security sha";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        jdkSHA1();
        bcSHA1();
        bcSHA224();
        ccSHA1();
    }
    public static void jdkSHA1(){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(src.getBytes());
            System.out.println("jdk SHA1:"+Hex.encodeHexString(md.digest()));

        } catch (Exception e) {
            // TODO: handle exception
        }

    }
    public static void bcSHA1(){
        Digest digest=new SHA1Digest();
        digest.update(src.getBytes(), 0, src.getBytes().length);;
        byte[] sha1Bytes=new byte[digest.getDigestSize()];
        digest.doFinal(sha1Bytes, 0);
        System.out.println("bc SHA1:"+org.bouncycastle.util.encoders.Hex.toHexString(sha1Bytes));
    }
    public static void bcSHA224(){
        Digest digest=new SHA224Digest();
        digest.update(src.getBytes(),0, src.getBytes().length);
        byte[] sha224Bytes=new byte[digest.getDigestSize()];
        digest.doFinal(sha224Bytes, 0);
        System.out.println("bc SHA224:"+org.bouncycastle.util.encoders.Hex.toHexString(sha224Bytes));
    }
    public static void ccSHA1(){
        System.out.println("cc SHA1-1:"+DigestUtils.sha1Hex(src.getBytes()));
        System.out.println("cc SHA1-2:"+DigestUtils.sha1Hex(src));
    }
}

3.MAC

package com.security3;

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

import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;

public class LoveMASC {
    private static String class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="imooc security mac";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        jdkHmacMD5();
        bcHmacMD5();
    }
   public static void jdkHmacMD5(){
       try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
        SecretKey secretKey=keyGenerator.generateKey();//产生密钥
        byte[] key=secretKey.getEncoded();//获得密钥
        SecretKey restoreSecretKey=new SecretKeySpec(key, "HmacMD5");//还原密钥
        Mac mac=Mac.getInstance(restoreSecretKey.getAlgorithm());//实例化MAC
        mac.init(restoreSecretKey);//初始化MAC
        byte[] hmacMD5Bytes=mac.doFinal(src.getBytes());//执行摘要
        System.out.println("jdk hmacMD5:"+Hex.toHexString(hmacMD5Bytes));
    } catch (Exception e) {
        // TODO: handle exception
    }
   }

   public static void bcHmacMD5(){
       HMac hmac=new HMac(new MD5Digest());
       hmac.init(new KeyParameter(Hex.decode("aaaaaaaaaa")));
       hmac.update(src.getBytes(), 0, src.getBytes().length);
       byte[] hmacMD5Bytes=new byte[hmac.getMacSize()];
       hmac.doFinal(hmacMD5Bytes, 0);
       System.out.println("ba hmacMD5:"+Hex.toHexString(hmacMD5Bytes));
   }
}
点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消