(java)检验下载的文件是否给别人篡改

在正规的网站下在文件的时候,有一个MD5值

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by moziqi on 15-4-23.
 * 消息摘要编码测试<br/>
 * <pre>
 *     文件为xxx
 *     存储路径目录
 *     MD5值为xxxx
 * </pre>
 */
public class MD5Test {

    //文件路径
    private String path = "";
    //MD5值
    private String md5 = "";

    @Before
    public void init() {
        path = "";
        md5 = "";
    }

    @Test
    public void testByMessageDisgest() {
        //构建文件输入流
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(path));
            try {
                //初始化MessageDigest,并指定MD5算法
                DigestInputStream digestInputStream = new DigestInputStream(fileInputStream, MessageDigest.getInstance("MD5"));
                //流缓冲大小
                int buf = 1024;
                //缓冲字节数组
                byte[] buffer = new byte[buf];
                //当读到值大于-1就继续读
                try {
                    int read = digestInputStream.read(buffer, 0, buf);
                    while (read > -1) {
                        read = digestInputStream.read(buffer, 0, buf);
                    }
                    //获得MessageDigest
                    MessageDigest messageDigest = digestInputStream.getMessageDigest();
                    //摘要处理
                    byte[] bytes = messageDigest.digest();
                    //十六进制转换
                    String md5Hex = Hex.encodeHexString(bytes);
                    Assert.assertEquals(md5Hex, md5);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    //关闭流
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testByDigestUitls() {
        //构建文件输入流
        FileInputStream fileInputStream = null;
        try {
            //构建文件输入流
            fileInputStream = new FileInputStream(new File(path));

            try {
                String md5Hex = DigestUtils.md5Hex(fileInputStream);
                Assert.assertEquals(md5Hex, md5);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                if (fileInputStream != null) {
                    //关闭流
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


转载于:https://my.oschina.net/moziqi/blog/405386

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值