Implement MD5 encryption of strings or files under Android

MD5-Information Digest Algorithm Brief Introduction

  • MD5 (Message-Digest Algorithm), a widely used cryptographic hash function, can generate a 128-bit (16-byte) hash value (hash value) to ensure the integrity and consistency of information transmission.
  • The calculated MD5 value may be repeated, but the probability is very low.
  • The encryption process is almost irreversible, unless a huge Key-Value database is maintained for collision cracking, it is almost impossible to unlock
  • The string encrypted by MD5 is fixed, and the result of each encryption of the same string or file remains unchanged

Code implementation under Android

  • The following code implements the general 32-bit md5 calculation, and the calculation results keep the generated characters as hexadecimal strings with a length of 32, which can be directly translated and used when needed in development
    public class Md5Util {
        public static String get(String text) {
            String result = null;
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] digest = md.digest(text.getBytes());
                result = toHexString(digest);
            } catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            }
            return result;
        }
    
        public static String get(InputStream inputStream) {
            String result = null;
            try {
                byte[] buffer = new byte[8192];
                int len;
                MessageDigest md = MessageDigest.getInstance("MD5");
                while ((len = inputStream.read(buffer)) != -1) {//分多次读入文件,占用内存比较少
                    md.update(buffer, 0, len);
                }
                inputStream.close();
    
                byte[] digest = md.digest();
                result = toHexString(digest);
            } catch (Exception ex) {
                ex.printStackTrace();
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return result;
        }
    
        public static String get(File file) {
            String result = null;
            try {
                result = get(new FileInputStream(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return result;
        }
    
        private static String toHexString(byte[] digest) {
            StringBuilder sb = new StringBuilder();
            String hexStr;
            for (byte b : digest) {
                hexStr = Integer.toHexString(b & 0xFF);//& 0xFF处理负数
                if (hexStr.length() == 1) {//长度等于1,前面进行补0,保证最后的字符串长度为32
                    hexStr = "0" + hexStr;
                }
                sb.append(hexStr);
            }
    
            return sb.toString();
        }
    }

Guess you like

Origin blog.csdn.net/qq_19942717/article/details/127555520