前言
java中获取文件md5值的四种方法其实都很类似,因为核心都是通过java自带的messagedigest类来实现。
获取文件md5值主要分为三个步骤,第一步获取文件的byte信息,第二步通过messagedigest类进行md5加密,第三步转换成16进制的md5码值。
什么是md5算法
md5讯息摘要演算法(英语:md5 message-digest algorithm),一种被广泛使用的密码杂凑函数,可以产生出一个128位元(16位元组)的散列值(hash value),用于确保信息传输完整一致。
原理
md5是一种不可逆的算法,是一种散列函数,使用的是hash算法。输入任意长度的信息,经过处理,输出为128位的信息(数字指纹),不同的输入得到的不同的结果(唯一性)。
在计算过程中原文的部分信息是丢失了的。所以不能从密文(散列值)反过来得到原文,即没有解密算法。
md5相当于超损压缩。
md5用途
1.防止被篡改:
1)比如发送一个电子文档,发送前,我先得到md5的输出结果a。然后在对方收到电子文档后,对方也得到一个md5的输出结果b。如果a与b一样就代表中途未被篡改。
2)比如我提供文件下载,为了防止不法分子在安装程序中添加木马,我可以在网站上公布由安装文件得到的md5输出结果。
3)svn在检测文件是否在checkout后被修改过,也是用到了md5.
2.防止直接看到明文:
- 现在很多网站在数据库存储用户的密码的时候都是存储用户密码的md5值。这样就算不法分子得到数据库的用户密码的md5值,也无法知道用户的密码。(比如在unix系统中用户的密码就是以md5(或其它类似的算法)经加密后存储在文件系统中。
- 当用户登录的时候,系统把用户输入的密码计算成md5值,然后再去和保存在文件系统中的md5值进行比较,进而确定输入的密码是否正确。通过这样的步骤,系统在并不知道用户密码的明码的情况下就可以确定用户登录系统的合法性。
- 这不但可以避免用户的密码被具有系统管理员权限的用户知道,而且还在一定程度上增加了密码被破解的难度。)
3.防止抵赖(数字签名):
- 这需要一个第三方认证机构。例如a写了一个文件,认证机构对此文件用md5算法产生摘要信息并做好记录。
- 若以后a说这文件不是他写的,权威机构只需对此文件重新产生摘要信息,然后跟记录在册的摘要信息进行比对,相同的话,就证明是a写的了。这就是所谓的“数字签名”。
md5安全性
普遍认为md5是很安全,因为暴力.破.解的时间是一般人无法接受的。实际上如果把用户的密码md5处理后再存储到数据库,其实是很不安全的。因为用户的密码是比较短的,而且很多用户的密码都使用生日,手机号码,身份证号码,电话号码等等。或者使用常用的一些吉利的数字,或者某个英文单词。
如果我把常用的密码先md5处理,把数据存储起来,然后再跟你的md5结果匹配,这时我就有可能得到明文。
现在大多数网站密码的策略是强制要求用户使用数字大小写字母的组合的方式提高用户密码的安全度。
方法
方法一
较为原始,将文件一次性读入内存,然后通过messagedigest进行md5加密,最后再手动将其转换为16进制的md5值。
private final static string[] strhex = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public static string getmd5one(string path) {
stringbuffer sb = new stringbuffer();
try {
messagedigest md = messagedigest.getinstance("md5");
byte[] b = md.digest(fileutils.readfiletobytearray(new file(path)));
for (int i = 0; i < b.length; i++) {
int d = b[i];
if (d < 0) {
d += 256;
}
int d1 = d / 16;
int d2 = d % 16;
sb.append(strhex[d1] + strhex[d2]);
}
} catch (nosuchalgorithmexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
return sb.tostring();
}
方法二
借助了integer类的方法实现16进制的转换,比方法一更简洁一些。
ps:java中byte是有负数的,代码中&0xff的操作与计算机中数据存储的原理有关,即负数存储的是二进制的补码。
public static string getmd5two(string path) {
stringbuffer sb = new stringbuffer("");
try {
messagedigest md = messagedigest.getinstance("md5");
md.update(fileutils.readfiletobytearray(new file(path)));
byte b[] = md.digest();
int d;
for (int i = 0; i < b.length; i++) {
d = b[i];
if (d < 0) {
d = b[i] & 0xff;
// 与上一行效果等同
// i += 256;
}
if (d < 16)
sb.append("0");
sb.append(integer.tohexstring(d));
}
} catch (nosuchalgorithmexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
return sb.tostring();
}
方法三
本方法在读入文件信息上有点不同。这里是分多次将一个文件读入,对于大型文件而言,比较推荐这种方式,占用内存比较少。
步骤三则是通过biginteger类提供的方法进行16进制的转换。
public static string getmd5three(string path) {
biginteger bi = null;
try {
byte[] buffer = new byte[8192];
int len = 0;
messagedigest md = messagedigest.getinstance("md5");
file f = new file(path);
fileinputstream fis = new fileinputstream(f);
while ((len = fis.read(buffer)) != -1) {
md.update(buffer, 0, len);
}
fis.close();
byte[] b = md.digest();
bi = new biginteger(1, b);
} catch (nosuchalgorithmexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
return bi.tostring(16);
}
方法四
通过调用 字节数组转十六进制字符串的方法 实现(跟方法3差不多)
public static string getmd5four(string filepath) {
try {
inputstream fis = new fileinputstream(filepath); // filenotfoundexception
messagedigest md = messagedigest.getinstance("md5"); // nosuchalgorithmexception
byte[] buffer = new byte[1024];
int length = -1;
while ((length = fis.read(buffer, 0, 1024)) != -1) { // io exception
md.update(buffer, 0, length);
}
fis.close();
// 转换并返回包含16个元素字节数组,返回数值范围为-128到127
byte[] md5bytes = md.digest();
// 方法2 使用biginteger
// biginteger biginteger = new biginteger(1, md5bytes);
// return biginteger.tostring(16);
// 方法3 使用字节数组转十六进制字符串
string strmd5 = bytestohexstr(md5bytes);
return strmd5;
} catch (filenotfoundexception e) {
e.printstacktrace();
return "";
} catch (nosuchalgorithmexception e) {
e.printstacktrace();
return "";
} catch (ioexception e) {
e.printstacktrace();
return "";
}
}
/**
* 字节数组转十六进制字符串
* @param b 字节数组
* @return 十六进制字符串
*/
public static string bytestohexstr(byte[] b) {
stringbuilder strbuilder = new stringbuilder();
string strtemp = "";
for (int n = 0; n < b.length; ++n) {
strtemp = (integer.tohexstring(b[n] & 0xff));
if (strtemp.length() == 1) {
strbuilder.append("0").append(strtemp);
} else {
strbuilder.append(strtemp);
}
}
return strbuilder.tostring();
}
方法五
最简单~
java自带的commons-codec包就提供了获取16进制md5值的方法。其底层实现上,也是分多次将一个文件读入,类似方法三、四
digestutils.md5hex(new fileinputstream(path));
另外,如何知道自己生成的文件md5值是否正确?
cmd 输入 powershell 切入到powershell面板
certutil -hashfile “./xxx.txt” md5 # (md5需大写)
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论