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

这段代码展示了如何使用MD5算法对文件、字符串、输入流和资源进行哈希值计算。通过读取数据并更新消息摘要,最终将十进制格式的字节转换为十六进制字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. File  

public String getFileMD5(File file)
{
    //Create checksum for this file
    //File file = new File("c:/temp/testOut.txt");

    //Use MD5 algorithm
    MessageDigest md5Digest = null;
    try
    {
        md5Digest = MessageDigest.getInstance("MD5");
    }
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }

    //Get the checksum
    String checksum = null;
    try
    {
        checksum = this.getFileChecksum(md5Digest, file);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    //see checksum
    //System.out.println(checksum);
    return checksum;
}

private static String getFileChecksum(MessageDigest digest, File file) throws IOException
{
    //Get file input stream for reading the file content
    FileInputStream fis = new FileInputStream(file);

    //Create byte array to read data in chunks
    byte[] byteArray = new byte[1024];
    int bytesCount = 0;

    //Read file data and update in message digest
    while ((bytesCount = fis.read(byteArray)) != -1)
    {
        digest.update(byteArray, 0, bytesCount);
    }

    //close the stream; We don't need it now.
    fis.close();

    //Get the hash's bytes
    byte[] bytes = digest.digest();

    //This bytes[] has bytes in decimal format;
    //Convert it to hexadecimal format
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < bytes.length; i++)
    {
        sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
    }

    //return complete hash
    return sb.toString();
}

2.String

public String getStringMD5(String str)
{
    //Use MD5 algorithm
    MessageDigest md5Digest = null;
    try
    {
        md5Digest = MessageDigest.getInstance("MD5");
    }
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }

    //Get the checksum
    String checksum = null;
    try
    {
        checksum = this.getStringChecksum(md5Digest, str);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    //see checksum
    //System.out.println(checksum);
    return checksum;
}

private static String getStringChecksum(MessageDigest digest, String str) throws IOException
{
    //Create byte array to read data in chunks
    byte[] byteArray = str.getBytes("utf-8");
    int bytesCount = byteArray.length ;

    //Read file data and update in message digest
    digest.update(byteArray, 0, bytesCount);

    //Get the hash's bytes
    byte[] bytes = digest.digest();

    //This bytes[] has bytes in decimal format;
    //Convert it to hexadecimal format
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < bytes.length; i++)
    {
        sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
    }

    //return complete hash
    return sb.toString();
}

3.InputStream

public String getInputStreamMD5(InputStream inputStream)
{
    //Create checksum for this file
    // File file = new File("c:/temp/testOut.txt");
    // try
    // {
    //     InputStream inputStream = new FileInputStream(file);
    // }
    // catch (FileNotFoundException e)
    // {
    //     e.printStackTrace();
    // }

    //Use MD5 algorithm
    MessageDigest md5Digest = null;
    try
    {
        md5Digest = MessageDigest.getInstance("MD5");
    }
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }

    //Get the checksum
    String checksum = null;
    try
    {
        checksum = this.getInputStreamChecksum(md5Digest, inputStream);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    //see checksum
    //System.out.println(checksum);
    return checksum;
}

private static String getInputStreamChecksum(MessageDigest digest, InputStream inputStream) throws IOException
{
    InputStream fis = inputStream;

    //Create byte array to read data in chunks
    byte[] byteArray = new byte[1024];
    int bytesCount = 0;

    //Read file data and update in message digest
    while ((bytesCount = fis.read(byteArray)) != -1)
    {
        digest.update(byteArray, 0, bytesCount);
    }

    //close the stream; We don't need it now.
    fis.close();

    //Get the hash's bytes
    byte[] bytes = digest.digest();

    //This bytes[] has bytes in decimal format;
    //Convert it to hexadecimal format
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < bytes.length; i++)
    {
        sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
    }

    //return complete hash
    return sb.toString();
}

4.Resource 

public String getResourceMD5(Resource resource)
{
    //Create checksum for this file
    // File file = new File("c:/temp/testOut.txt");
    // Resource resource = new FileSystemResource(file);
    
    //Use MD5 algorithm
    MessageDigest md5Digest = null;
    try
    {
        md5Digest = MessageDigest.getInstance("MD5");
    }
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }

    //Get the checksum
    String checksum = null;
    try
    {
        checksum = this.getResourceChecksum(md5Digest, resource);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    //see checksum
    //System.out.println(checksum);
    return checksum;
}

private static String getResourceChecksum(MessageDigest digest, Resource resource) throws IOException
{
    //Get  inputstream from resource
    InputStream fis = resource.getInputStream();

    //Create byte array to read data in chunks
    byte[] byteArray = new byte[1024];
    int bytesCount = 0;

    //Read file data and update in message digest
    while ((bytesCount = fis.read(byteArray)) != -1)
    {
        digest.update(byteArray, 0, bytesCount);
    }

    //close the stream; We don't need it now.
    fis.close();

    //Get the hash's bytes
    byte[] bytes = digest.digest();

    //This bytes[] has bytes in decimal format;
    //Convert it to hexadecimal format
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < bytes.length; i++)
    {
        sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
    }

    //return complete hash
    return sb.toString();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值