• 对指定文件生成数字摘要的MD5工具类


    md5特点:压缩性、不可逆性,经常用于传值过程中的值加密或文件加密
    static char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    public static String getMD5(File file) {

    FileInputStream fis = null;
    try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    fis = new FileInputStream(file);
    byte[] buffer = new byte[2048];
    int length = -1;
    while ((length = fis.read(buffer)) != -1) {
    md.update(buffer, 0, length);
    }
    //32位加密
    byte[] b = md.digest();
    return byteToHexString(b);

    } catch (Exception ex) {
    ex.printStackTrace();
    return null;
    }
    finally {
    try {
    fis.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    }


    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;
    }

  • 相关阅读:
    时间安排还是很不合理
    ZOJ Problem Set
    ZOJ Problem Set
    ZOJ Problem Set
    ZOJ Problem Set
    还是很水啊!!!
    pragma mark
    IOS应用发布NSLog的如何注释
    设定时间让应用从后台回来是否重新启动应用
    Mac 上SVN上传.a文件
  • 原文地址:https://www.cnblogs.com/yolanda-lee/p/4974874.html
Copyright © 2020-2023  润新知