zoukankan      html  css  js  c++  java
  • 使用JDK自带的MessageDigest计算消息摘要

    使用JDK自带的MessageDigest计算消息摘要

    上代码

    /**
     * 使用JDK自带MessageDigest
     */
    public class MessageDigestUtils {
        /**
         * 计算消息摘要
         * @param algorithm 消息摘要算法
         * @param in 数据流
         * @return 消息摘要
         * @throws IOException
         * @throws NoSuchAlgorithmException
         */
        public static byte[] digest(String algorithm, InputStream in)
            throws IOException, NoSuchAlgorithmException {
            
            MessageDigest md = MessageDigest.getInstance(algorithm);
            
            try {
                byte[] buf = new byte[4096];
                int r;
                while ((r = in.read(buf)) != -1) {
                    md.update(buf, 0, r);
                }
            } finally {
                in.close();
            }
            
            return md.digest();
        }
        
        public static byte[] digest(String algorithm, File file)
            throws NoSuchAlgorithmException, FileNotFoundException, IOException {
            
            return digest(algorithm, new FileInputStream(file));
        }
        
        public static byte[] digest(String algorithm, String text, String encoding)
            throws NoSuchAlgorithmException, UnsupportedEncodingException, IOException {
            
            return digest(algorithm, new ByteArrayInputStream(text.getBytes(encoding)));
        }
    }
  • 相关阅读:
    java动态代理机制
    Spring的几种注入bean的方式
    java的泛型与反射机制
    java中equals与==的比较
    Java虚拟机JVM简单理解
    java集合类总结
    timersmanager 解析
    rtsp实时流通过rtmp推送到服务端
    udp 视频包网络传输花屏
    GB28181国检推流
  • 原文地址:https://www.cnblogs.com/zyunx/p/7081356.html
Copyright © 2011-2022 走看看