zoukankan      html  css  js  c++  java
  • MD5工具类

    /**
     * MD5加密工具类
     * <p>
     * 参考链接:http://www.cnblogs.com/whoislcj/p/5885006.html
     */
    public final class Md5Util {
    
        private Md5Util() {
            throw new UnsupportedOperationException("cannot be instantiated");
        }
    
        /**
         * 计算字符串的 MD5 值
         *
         * @param plaintext 明文
         * @return 密文
         */
        public static String encrypt(String plaintext) {
            if (TextUtils.isEmpty(plaintext)) {
                return "";
            }
            try {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                byte[] bytes = md5.digest(plaintext.getBytes());
                String result = "";
                for (byte b : bytes) {
                    String temp = Integer.toHexString(b & 0xff);
                    if (temp.length() == 1) {
                        temp = "0" + temp;
                    }
                    result += temp;
                }
                return result;
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            return "";
        }
    
        /**
         * 计算文件的 MD5 值
         *
         * @param file 需要加密的文件
         * @return 加密后的数据
         */
        public static String encrypt(File file) {
            if (file == null || !file.isFile() || !file.exists()) {
                return "";
            }
            FileInputStream in = null;
            String result = "";
            byte buffer[] = new byte[8192];
            int len;
            try {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                in = new FileInputStream(file);
                while ((len = in.read(buffer)) != -1) {
                    md5.update(buffer, 0, len);
                }
                byte[] bytes = md5.digest();
    
                for (byte b : bytes) {
                    String temp = Integer.toHexString(b & 0xff);
                    if (temp.length() == 1) {
                        temp = "0" + temp;
                    }
                    result += temp;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != in) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return result;
        }
    
        /**
         * 采用nio方式进行 MD5 加密
         *
         * @param file 需要加密的文件
         * @return 加密后的数据
         */
        public static String encryptByNio(File file) {
            String result = "";
            FileInputStream in = null;
            try {
                in = new FileInputStream(file);
                MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(byteBuffer);
                byte[] bytes = md5.digest();
                for (byte b : bytes) {
                    String temp = Integer.toHexString(b & 0xff);
                    if (temp.length() == 1) {
                        temp = "0" + temp;
                    }
                    result += temp;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != in) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return result;
        }
    
        /**
         * 对字符串进行对此 MD5 加密,提高安全性
         *
         * @param string 需要加密的数据
         * @param times  加密次数
         * @return 加密后的数据
         */
        public static String encrypt(String string, int times) {
            if (TextUtils.isEmpty(string)) {
                return "";
            }
            String md5 = encrypt(string);
            for (int i = 0; i < times - 1; i++) {
                md5 = encrypt(md5);
            }
            return encrypt(md5);
        }
    
        /**
         * MD5加盐
         * <p>
         * string + key(盐值 key)然后进行 MD5 加密
         *
         * @param string 需要加密的数据
         * @param slat 盐
         * @return 加密后的数据
         */
        public static String encrypt(String string, String slat) {
            if (TextUtils.isEmpty(string)) {
                return "";
            }
            MessageDigest md5 = null;
            try {
                md5 = MessageDigest.getInstance("MD5");
                byte[] bytes = md5.digest((string + slat).getBytes());
                String result = "";
                for (byte b : bytes) {
                    String temp = Integer.toHexString(b & 0xff);
                    if (temp.length() == 1) {
                        temp = "0" + temp;
                    }
                    result += temp;
                }
                return result;
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            return "";
        }
    }
  • 相关阅读:
    ABB机器人 带参数例行程序
    面试题10- I:斐波那契数列(C++)
    面试题39:数组中出现次数超过一半的数字(C++)
    面试题50:第一个只出现一次的字符(C++)
    第八部分 表的基本操作
    第七部分 表中数据的基本操作
    面试题18:删除链表的节点(C++)
    面试题35:复杂链表的复制(C++)
    面试题54:二叉搜索树的第k大节点(C++)
    面试题62:圆圈中最后剩下的数字(C++)
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/11613466.html
Copyright © 2011-2022 走看看