java md5 sha256
省流
String md5 = DigestUtils.md5Hex(inputStream);
String md5 = DigestUtils.md5Hex(str);
String md5 = DigestUtils.md5Hex(byteArray);
这是commons-codec.jar
JAVA中获取文件MD5值的四种方法 - 腾讯云开发者社区-腾讯云 (tencent.com)
一般上传文件,会用文件的md5作为文件名字,防止发生冲突。
guava:
“ 如果必须与需要 MD5 的系统进行互操作,请使用此方法,尽管它已弃用。但是,如果您可以选择哈希函数,请避免使用 MD5,它既不快速也不安全。截至 2017 年 1 月,我们建议: 为了安全起见:sha256 或更高级别的 API。 为了速度:使用 goodFastHash
”
跟着 Guava 学 java 之 Hashing - 知乎 (zhihu.com)
10-散列 | Google Guava 中文教程 (gitbooks.io)
guava 对文件hash
@Test
public void givenFile_whenChecksumUsingGuava_thenVerifying()
throws IOException {
String filename = "src/test/resources/test_md5.txt";
String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
HashCode hash = com.google.common.io.Files
.hash(new File(filename), Hashing.md5());
String myChecksum = hash.toString().toUpperCase();
assertThat(myChecksum.equals(checksum)).isTrue();
}
MD5 Hashing in Java | Baeldung
guava md5 sha1 sha256
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.junit.jupiter.api.Test;
public class T1 {
@Test
public void GuavaHash(){
String token = "123456789";
//md5
String md5 = Hashing.md5().newHasher().putString(token, Charsets.UTF_8).hash().toString();
//sha1
String sha1 = Hashing.sha1().newHasher().putString(token, Charsets.UTF_8).hash().toString();
//sha256
String sha256 = Hashing.sha256().newHasher().putString(token, Charsets.UTF_8).hash().toString();
System.out.println(md5);//32个字符
System.out.println(sha1);//40个字符
System.out.println(sha256);//64个字符
}
}
源码
public static String copyInputStreamToFileAndGetMd5Hex(InputStream inputStream, File file) throws IOException {
FileUtils.copyInputStreamToFile(inputStream, file);
return DigestUtils.md5Hex(new FileInputStream(file));
}
DigestUtils源码,InputStream拆成长度1024的字节数组逐个MD5
public static MessageDigest updateDigest(final MessageDigest digest, final InputStream data) throws IOException {
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
while (read > -1) {
digest.update(buffer, 0, read);
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
}
return digest;
}
FileUtils.copyInputStreamToFile的源码,同样也是将InputStream拆成4096的字节数组,逐个写到目标文件中
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
long count;
int n;
for (count = 0L; -1 != (n = input.read(buffer)); count += (long) n) {
output.write(buffer, 0, n);
}
return count;
}
public static String copyInputStreamToFileAndGetMd5Hex(InputStream inputStream, File file) throws IOException {
MessageDigest digest = DigestUtils.getMd5Digest();
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[2048];
int read = inputStream.read(buffer);
while (read > -1) {
// 计算MD5,顺便写到文件
digest.update(buffer, 0, read);
outputStream.write(buffer, 0, read);
read = inputStream.read(buffer);
}
} finally {
IOUtils.closeQuietly(outputStream);
}
return Hex.encodeHexString(digest.digest());
}
java计算md5 文件(File)、字符串(String)、输入流(InputStream)、资源(Resource)
hutool源码
public byte[] digest(InputStream data) {
return this.digest(data, 8192);
}
public byte[] digest(InputStream data, int bufferLength) throws IORuntimeException {
byte[] result;
if (ArrayUtil.isEmpty(this.salt)) {
result = this.digestWithoutSalt(data, bufferLength);
} else {
result = this.digestWithSalt(data, bufferLength);
}
return this.resetAndRepeatDigest(result);
}
循环读取InputStream,读取长度是8192,
private byte[] digestWithoutSalt(InputStream data, int bufferLength) throws IOException {
final byte[] buffer = new byte[bufferLength];
int read;
while ((read = data.read(buffer, 0, bufferLength)) > -1) {
this.digest.update(buffer, 0, read);
}
return this.digest.digest();
}
this.digest就是java.security.MessageDigest