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)));
        }
    }
  • 相关阅读:
    Hello, Fedora.
    Android与Linux分道扬镳
    VIM教程V1.5梁昌泰
    强大的NTFS文件系统
    Linux下的cc与gcc
    g++与gcc的区别
    Fedora下解压缩的相关问题
    The GNU C Reference Manual
    Linux Kbuild文档
    实验一:计算机是怎样工作的
  • 原文地址:https://www.cnblogs.com/zyunx/p/7081356.html
Copyright © 2011-2022 走看看