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

    抽时间写了一个算法工具类,目前支持的算法有SHA1,SHA256,SHA384,SHA512,MD5,同时支持获取文件的MD5值。

    使用方法如下:

    获取字符串的MD5值

     String str= AlgorithmHelper.getHash("123", AlgorithmHelper.MD5)

    获取字符串的SHA256值

      String str= AlgorithmHelper.getHash("123", AlgorithmHelper.SHA256);

    获取文件的MD5值

     File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"1.jpg");
     String str=AlgorithmHelper.getMD5(file);

    代码文件如下:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.HashMap;
    import java.util.Map;
    
    public class AlgorithmHelper {
    
        private final static Map<Integer, String> mapList = new HashMap<Integer, String>();
        private final static char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
    
        public final static int SHA1 = 0;
        public final static int SHA256 = 1;
        public final static int SHA384 = 2;
        public final static int SHA512 = 3;
        public final static int MD5 = 4;
    
        static {
            mapList.put(SHA1, "SHA-1");
            mapList.put(SHA256, "SHA-256");
            mapList.put(SHA384, "SHA-384");
            mapList.put(SHA512, "SHA-512");
            mapList.put(MD5, "MD5");
        }
    
    
        private static String convertToHex(byte[] bytes) {
            char[] hexChars = new char[bytes.length * 2];
            for (int j = 0; j < bytes.length; j++) {
                int v = bytes[j] & 0xFF;
                hexChars[j * 2] = HEX_CHARS[v >>> 4];
                hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
            }
            return new String(hexChars);
        }
    
        public static String getHash(String text, int algorithm) {
            String hash = null;
            try {
                if (mapList.containsKey(algorithm)) {
                    final MessageDigest digest = MessageDigest.getInstance(mapList.get(algorithm));
                    final byte[] bytes = text.getBytes("UTF-8");
                    digest.update(bytes, 0, bytes.length);
                    hash = convertToHex(digest.digest());
                }
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return hash;
        }
    
        //获取字符串的MD5值
        public static String getMD5(String text) {
            return getHash(text, MD5);
        }
    
        //获取文件的MD5值
        public static String getMD5(File file) {
            String hash = null;
            if (file == null || !file.exists()) {
                return hash;
            }
    
            InputStream is = null;
            try {
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                is = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int len = -1;
                while ((len = is.read(buffer)) != -1) {
                    messageDigest.update(buffer, 0, len);
                }
                byte[] bytes = messageDigest.digest();
                hash = convertToHex(bytes);
            } catch (Exception e) {
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return hash;
        }
    }
  • 相关阅读:
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊(分块)
    BZOJ 2648 SJY摆棋子(KD Tree)
    Codeforces Round #441 D. Sorting the Coins(模拟)
    HDU 3400 Line belt (三分套三分)
    HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)
    HDU 5985 Lucky Coins(概率)
    HDU 5988 Coding Contest(浮点数费用流)
    HDU 5792 World is Exploding(树状数组+离散化)
    HDU 5791 Two(LCS求公共子序列个数)
    HDU 5787 K-wolf Number(数位dp)
  • 原文地址:https://www.cnblogs.com/rainboy2010/p/6525650.html
Copyright © 2011-2022 走看看