-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHash.java
More file actions
117 lines (104 loc) · 4.2 KB
/
FileHash.java
File metadata and controls
117 lines (104 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
public class FileHash {
public static void main(String[] args) {
try {
//本机jdk源码文件的MD5值
String filePath = "C:\\Program Files\\Java\\jdk1.8.0_261\\src.zip";
String md5Hashcode = md5HashCode(filePath);
String md5Hashcode32 = md5HashCode32(filePath);
System.out.println(md5Hashcode + ":文件的md5值");
System.out.println(md5Hashcode32+":文件32位的md5值");
//System.out.println(-100 & 0xff);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取文件的md5值 ,有可能不是32位
* @param filePath 文件路径
* @return
* @throws FileNotFoundException
*/
public static String md5HashCode(String filePath) throws FileNotFoundException{
FileInputStream fis = new FileInputStream(filePath);
return md5HashCode(fis);
}
/**
* 保证文件的MD5值为32位
* @param filePath 文件路径
* @return
* @throws FileNotFoundException
*/
public static String md5HashCode32(String filePath) throws FileNotFoundException{
FileInputStream fis = new FileInputStream(filePath);
return md5HashCode32(fis);
}
/**
* java获取文件的md5值
* @param fis 输入流
* @return
*/
public static String md5HashCode(InputStream fis) {
try {
//拿到一个MD5转换器,如果想使用SHA-1或SHA-256,则传入SHA-1,SHA-256
MessageDigest md = MessageDigest.getInstance("MD5");
//分多次将一个文件读入,对于大型文件而言,比较推荐这种方式,占用内存比较少。
byte[] buffer = new byte[1024];
int length = -1;
while ((length = fis.read(buffer, 0, 1024)) != -1) {
md.update(buffer, 0, length);
}
fis.close();
//转换并返回包含16个元素字节数组,返回数值范围为-128到127
byte[] md5Bytes = md.digest();
BigInteger bigInt = new BigInteger(1, md5Bytes);//1代表绝对值
return bigInt.toString(16);//转换为16进制
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* java计算文件32位md5值
* @param fis 输入流
* @return
*/
public static String md5HashCode32(InputStream fis) {
try {
//拿到一个MD5转换器,如果想使用SHA-1或SHA-256,则传入SHA-1,SHA-256
MessageDigest md = MessageDigest.getInstance("MD5");
//分多次将一个文件读入,对于大型文件而言,比较推荐这种方式,占用内存比较少。
byte[] buffer = new byte[1024];
int length = -1;
while ((length = fis.read(buffer, 0, 1024)) != -1) {
md.update(buffer, 0, length);
}
fis.close();
//转换并返回包含16个元素字节数组,返回数值范围为-128到127
byte[] md5Bytes = md.digest();
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;//解释参见最下方
if (val < 16) {
/**
* 如果小于16,那么val值的16进制形式必然为一位,
* 因为十进制0,1...9,10,11,12,13,14,15 对应的 16进制为 0,1...9,a,b,c,d,e,f;
* 此处高位补0。
*/
hexValue.append("0");
}
//这里借助了Integer类的方法实现16进制的转换
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}