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)));
        }
    }
  • 相关阅读:
    docker架构的详解
    docker的核心原理-cgroup
    网络运维面试题
    100道linux运维笔试题
    运维岗位面试题集合
    python——筛子游戏
    同道前辈
    delphi中使用SocketStream读写数据的技巧
    百度地图API
    HTML中小meta的大作用
  • 原文地址:https://www.cnblogs.com/zyunx/p/7081356.html
Copyright © 2011-2022 走看看