java md5 sha256

╰+哭是因爲堅強的太久メ 2024-03-22 08:41 120阅读 0赞

省流

  1. String md5 = DigestUtils.md5Hex(inputStream);
  2. String md5 = DigestUtils.md5Hex(str);
  3. 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

  1. @Test
  2. public void givenFile_whenChecksumUsingGuava_thenVerifying()
  3. throws IOException {
  4. String filename = "src/test/resources/test_md5.txt";
  5. String checksum = "5EB63BBBE01EEED093CB22BB8F5ACDC3";
  6. HashCode hash = com.google.common.io.Files
  7. .hash(new File(filename), Hashing.md5());
  8. String myChecksum = hash.toString().toUpperCase();
  9. assertThat(myChecksum.equals(checksum)).isTrue();
  10. }

MD5 Hashing in Java | Baeldung

guava md5 sha1 sha256

  1. import com.google.common.base.Charsets;
  2. import com.google.common.hash.Hashing;
  3. import org.junit.jupiter.api.Test;
  4. public class T1 {
  5. @Test
  6. public void GuavaHash(){
  7. String token = "123456789";
  8. //md5
  9. String md5 = Hashing.md5().newHasher().putString(token, Charsets.UTF_8).hash().toString();
  10. //sha1
  11. String sha1 = Hashing.sha1().newHasher().putString(token, Charsets.UTF_8).hash().toString();
  12. //sha256
  13. String sha256 = Hashing.sha256().newHasher().putString(token, Charsets.UTF_8).hash().toString();
  14. System.out.println(md5);//32个字符
  15. System.out.println(sha1);//40个字符
  16. System.out.println(sha256);//64个字符
  17. }
  18. }

源码

  1. public static String copyInputStreamToFileAndGetMd5Hex(InputStream inputStream, File file) throws IOException {
  2. FileUtils.copyInputStreamToFile(inputStream, file);
  3. return DigestUtils.md5Hex(new FileInputStream(file));
  4. }

DigestUtils源码,InputStream拆成长度1024的字节数组逐个MD5

  1. public static MessageDigest updateDigest(final MessageDigest digest, final InputStream data) throws IOException {
  2. final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
  3. int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
  4. while (read > -1) {
  5. digest.update(buffer, 0, read);
  6. read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
  7. }
  8. return digest;
  9. }

FileUtils.copyInputStreamToFile的源码,同样也是将InputStream拆成4096的字节数组,逐个写到目标文件中

  1. public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
  2. long count;
  3. int n;
  4. for (count = 0L; -1 != (n = input.read(buffer)); count += (long) n) {
  5. output.write(buffer, 0, n);
  6. }
  7. return count;
  8. }
  9. public static String copyInputStreamToFileAndGetMd5Hex(InputStream inputStream, File file) throws IOException {
  10. MessageDigest digest = DigestUtils.getMd5Digest();
  11. FileOutputStream outputStream = null;
  12. try {
  13. outputStream = new FileOutputStream(file);
  14. byte[] buffer = new byte[2048];
  15. int read = inputStream.read(buffer);
  16. while (read > -1) {
  17. // 计算MD5,顺便写到文件
  18. digest.update(buffer, 0, read);
  19. outputStream.write(buffer, 0, read);
  20. read = inputStream.read(buffer);
  21. }
  22. } finally {
  23. IOUtils.closeQuietly(outputStream);
  24. }
  25. return Hex.encodeHexString(digest.digest());
  26. }

java计算md5 文件(File)、字符串(String)、输入流(InputStream)、资源(Resource)

hutool源码

  1. public byte[] digest(InputStream data) {
  2. return this.digest(data, 8192);
  3. }
  4. public byte[] digest(InputStream data, int bufferLength) throws IORuntimeException {
  5. byte[] result;
  6. if (ArrayUtil.isEmpty(this.salt)) {
  7. result = this.digestWithoutSalt(data, bufferLength);
  8. } else {
  9. result = this.digestWithSalt(data, bufferLength);
  10. }
  11. return this.resetAndRepeatDigest(result);
  12. }

循环读取InputStream,读取长度是8192,

  1. private byte[] digestWithoutSalt(InputStream data, int bufferLength) throws IOException {
  2. final byte[] buffer = new byte[bufferLength];
  3. int read;
  4. while ((read = data.read(buffer, 0, bufferLength)) > -1) {
  5. this.digest.update(buffer, 0, read);
  6. }
  7. return this.digest.digest();
  8. }

this.digest就是java.security.MessageDigest

发表评论

表情:
评论列表 (有 0 条评论,120人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SHA256Md5区别

    SHA256和MD5都是常用的散列函数。它们都用于生成消息摘要,但是SHA256具有更高的安全性。 MD5是一种128位的散列函数,生成的消息摘要长度为128位。它被广泛用于