当前位置: 首页 > article >正文

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


http://www.kler.cn/a/13975.html

相关文章:

  • 一分钟了解美国棒球体系·棒球1号位
  • LVS负载均衡之DR模式
  • 汽车音响进入致臻全景声时代,丹拿瞄准了两大行业痛点
  • 大学生蓝桥杯
  • 算法刷题|139.单词拆分、多重背包
  • 购物 · 礼物
  • 【Buildroot】基础知识:目录、根文件系统目录覆盖、编译性能分析(编译时间、目标尺寸、包依赖图)
  • YOLOv7+单目实现三维跟踪(python)
  • Java双亲委派和类加载器
  • springboot+vue小区物业管理系统(源码+文档)
  • XML 简介
  • 数据仓库与数据建模理论
  • Linux系统应用编程(五)Linux网络编程(上篇)
  • 大四的告诫
  • 免费gpt-4-国内使用gpt-4
  • 卷积神经网络(CNN)简单介绍,给出实例并添加详细的注释
  • Java八大基本数据类型
  • CentOS系统安装Intel E810 25G网卡驱动
  • PPOCR -训练模型转推理模型遇到的问题
  • 打造卓越游戏 | 2023 Google 游戏开发者峰会