zoukankan      html  css  js  c++  java
  • [JAVA]MD5加密

    import java.io.*;
    import java.math.BigInteger;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    /**
     * @description:
     * @projectName:HelloWorld
     * @see:PACKAGE_NAME
     * @author:郑晓龙
     * @createTime:2019/5/15 8:48
     * @version:1.0
     */
    public class MD5DigestTest {
        public static void main(String[] args) {
            System.out.println(stringMd5Hex("admin"));
            long t1 = System.currentTimeMillis();
            System.out.println(fileMd5Hex("e:/CentOS-7-x86_64-DVD-1810.iso"));
            System.out.println("timing:"+(System.currentTimeMillis()-t1));
    
    
        }
    
        public static String stringMd5Hex(String str) {
    
            MessageDigest md5 = null;
            try {
                md5 = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            BigInteger bigInt = new BigInteger(1, md5.digest(str.getBytes()));
            return bigInt.toString(16);
    
        }
    
        public static String fileMd5Hex(String fileName) {
    
            MessageDigest md = null;
            try (InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(fileName)))) {
                md = MessageDigest.getInstance("MD5");
                byte[] buf = new byte[1024*10];
                int len;
                while ((len = inputStream.read(buf)) != -1) {
                    md.update(buf, 0, len);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
    
            BigInteger bigInt = new BigInteger(1, md.digest());
            return bigInt.toString(16);
        }
    }
  • 相关阅读:
    AcWing 1018. 最低通行费
    蓝桥杯赛第10届省赛
    P5745 【深基附B例】区间最大和
    P3383 【模板】线性筛素数
    第12届蓝桥杯赛国赛 小蓝买瓜子
    P4715 【深基16.例1】淘汰赛
    AcWing 1015. 摘花生
    第12届蓝桥杯赛省赛 种菜的最大价值
    linq to sql初步
    汇编语言学习笔记接收鼠标消息
  • 原文地址:https://www.cnblogs.com/zhengxl5566/p/10870896.html
Copyright © 2011-2022 走看看