廖雪峰Java10加密与安全-3摘要算法-4BouncyCastle

时间:2019-05-10 19:01:30   收藏:0   阅读:215

1.BouncyCastle:

2.如何使用第三方提供的算法

    public static void main(String[] args) throws Exception{
        Security.addProvider(new BouncyCastleProvider());
        MessageDigest md = MessageDigest.getInstance("RipeMD160");
        String s = "hello world";
        md.update(s.getBytes("UTF-8"));
        System.out.println(md.digest().length);
        System.out.println(String.format("%040x",new BigInteger(1,md.digest())));
    }

技术分享图片

3.代码示例

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.Security;

public class SplitString {
    public static void digest(String hashAlggorithm, byte[] input){
        Security.addProvider(new BouncyCastleProvider());
        MessageDigest md;
        try{
            md = MessageDigest.getInstance(hashAlggorithm);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        md.update(input);
        System.out.println(md.digest().length);
        System.out.println(String.format("%0"+md.digest().length*2+"x",new BigInteger(1,md.digest())));
        
    }
    public static void main(String[] args) throws Exception{
        String s = "hello 妹子";
        byte[] bs = s.getBytes("UTF-8");
        digest("MD5",bs);
        digest("SHA-1",bs);
        digest("RipeMD160",bs);
    }
}

技术分享图片

4.总结

原文:https://www.cnblogs.com/csj2018/p/10846115.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!