• Android:MD5加密


    /**
     * @author gongchaobin
     * 
     * MD5加密
     * 
     * @version 2013-8-22
     */
    public class MD5Util {
        // 用来将字节转换成 16 进制表示的字符
        static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    
        public static String getFileMD5(InputStream fis) {
            MessageDigest md = null;
            byte[] buffer = new byte[2048];
            int length = -1;
            long s = System.currentTimeMillis();
            try {
                md = MessageDigest.getInstance("MD5");
                while ((length = fis.read(buffer)) != -1) {
                    md.update(buffer, 0, length);
                }
                
                System.err.println("last: " + (System.currentTimeMillis() - s));
                byte[] b = md.digest();
                return byteToHexStringSingle(b);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            return null;
        }
    
        /**
         * /** 对文件全文生成MD5摘要
         * 
         * @param file
         *            要加密的文件
         * @return MD5摘要码
         * @throws FTPException
         */
        public static String getFileMD5(File file) {
            try {
                return getFileMD5(new FileInputStream(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /** */
        /**
         * 对一段String生成MD5加密信息
         * 
         * @param message
         *            要加密的String
         * @return 生成的MD5信息
         */
        public static String getMD5(String message) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                System.err.println("MD5摘要长度:" + md.getDigestLength());
                byte[] b = md.digest(message.getBytes("utf-8"));
                return byteToHexStringSingle(b);// byteToHexString(b);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Deprecated
        /** */
        /** 
         * 把byte[]数组转换成十六进制字符串表示形式 
         * @param tmp    要转换的byte[] 
         * @return 十六进制字符串表示形式 
         */
        private static String byteToHexString(byte[] tmp) {
            String s;
            // 用字节表示就是 16 个字节
            char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符,
            // 所以表示成 16 进制需要 32 个字符
            int k = 0; // 表示转换结果中对应的字符位置
            for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节
                // 转换成 16 进制字符的转换
                byte byte0 = tmp[i]; // 取第 i 个字节
                str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
                // >>> 为逻辑右移,将符号位一起右移
                str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
            }
            s = new String(str); // 换后的结果转换为字符串
            return s;
        }
    
        /**
         * 独立把byte[]数组转换成十六进制字符串表示形式
         * 
         * @author Bill
         * @create 2010-2-24 下午03:26:53
         * @since
         * @param byteArray
         * @return
         */
        public static String byteToHexStringSingle(byte[] byteArray) {
            StringBuffer md5StrBuff = new StringBuffer();
    
            for (int i = 0; i < byteArray.length; i++) {
                if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                    md5StrBuff.append("0").append(
                            Integer.toHexString(0xFF & byteArray[i]));
                else
                    md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
            }
    
            return md5StrBuff.toString();
        }
    }
  • 相关阅读:
    些许注意事项(初学)
    第一个方法(初学)
    重载(初学)
    数组(初学)
    文件下载(初学)
    javaweb中上传视频,并且播放,用上传视频信息为例
    javaweb中上传图片并显示图片,用我要上传课程信息(里面包括照片)这个例子说明
    从后台servlet中,获取jsp页面输入的值,来删除用户一行信息
    第二式 观察者模式
    第一式 策略模式
  • 原文地址:https://www.cnblogs.com/gongcb/p/3274674.html
Copyright © 2020-2023  润新知