简体   繁体   English

Java中有关MD5校验和计算的说明

[英]clarification regarding MD5 checksum calculation in java

I have used the code in this stackoverflow discussion in order to calculate the checksum of a file in java. 我已在此stackoverflow讨论中使用了代码,以便计算java中文件的校验和。

I am a little confused about this working I am applying this in my problem as follows : 我对此工作有些困惑,我将其应用于我的问题,如下所示:

I have a file with some data. 我有一些数据文件。 I have calculated the size of the text in the file using 我已经使用计算了文件中文本的大小

  System.out.println(file1content.toString().getBytes().length);  the o/p is 4096 bytes

When i try to execute the checksum code I realize that the number of bytes being read is 4096+12 bytes, is this 12 bytes equal to the filename ? 当我尝试执行校验和代码时,我意识到正在读取的字节数是4096 + 12字节,这12个字节等于文件名吗?

I have another file2 with the same content as file1 ( i know this for sure because I extract the text to a String and compare it with String.equals) but the checksum generated is different. 我有另一个文件2,其内容与文件1相同(我肯定知道这一点,因为我将文本提取到字符串中并与String.equals进行了比较),但是生成的校验和不同。 I am wondering why this is happening ? 我想知道为什么会这样吗?

Am I missing something here ? 我在这里想念什么吗?

Edit 1: 编辑1:

I am reading data from the file using the following loop : 我正在使用以下循环从文件读取数据:

 InputStream fis =  new FileInputStream(filename);
 byte[] buffer = new byte[1024];
  do {
       numRead = fis.read(buffer);
       System.out.println(" "+ numRead);
       if (numRead > 0) {
           complete.update(buffer, 0, numRead);
       }
   } while (numRead != -1);

   fis.close();

The output of numread is : numread的输出是:

 1024
 1024
 1024
 1024
 12
 -1

Regards, Bhavya 问候,Bhavya

Well I found out what the bug was, I am not sure if this I introduced the bug or if it was already there. 好吧,我发现了错误,我不确定这是我引入的错误还是已经存在。

I realised that the data being read from the file was not correct, some portions of the file were read multiple times, so I modified the code so that I could obtain data from the file by specifying the start and end positions. 我意识到从文件中读取的数据不正确,多次读取了文件的某些部分,因此我修改了代码,以便可以通过指定开始和结束位置从文件中获取数据。

In case anyone is facing this issue please let me know I can post the solution for this. 如果有人遇到此问题,请让我知道我可以为此发布解决方案。

Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM