【发布时间】:2014-02-10 03:04:10
【问题描述】:
我正在尝试从内存中读取文件并将其拆分为 1KB 的块。
程序所做的是从内存中读取一个文件(视频文件),然后将其拆分为 1KB 的块。然后它使用 SHA-256 对最后一个块进行散列,并将散列附加到倒数第二个块。然后它计算倒数第二个块的哈希值和附加的哈希值,然后将此哈希值附加到它的前一个块上。这一直持续到第一个块,它将附加第二个块的哈希。
我只需要第一个块的哈希值及其附加的哈希值。我试图以两种方式实现这一点,但我认为我做错了。有人可以告诉我我在哪里做错了。我已经坚持了 6 天没有解决方案。我在下面粘贴了我的两个实现。任何帮助将不胜感激。
我已阅读整个文件,并尝试在下面的尝试中手动将字节数组拆分为 1KB 块。
package com.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReadFileByteByByte {
public static void main(String[] args) throws Exception {
InputStream inStream = null;
BufferedInputStream bis = null;
try{
inStream = new FileInputStream("C:\\a.mp4");
bis = new BufferedInputStream(inStream);
int numByte = bis.available();
byte[] buf = new byte[numByte];
bis.read(buf, 0, numByte);
System.out.println(numByte/1024);
ArrayList<byte[]> a = new ArrayList<>();
ArrayList<byte[]> b = new ArrayList<>();
for(int i=0,j=0;i<buf.length;i++,j++){
byte[] buf2 = new byte[1057];
buf2[j] = buf[i];
if(i%1024==1023){
a.add(buf2);
j=0;
}
}
for(int i=a.size()-1,j=-1;i>=0;i--,j++){
MessageDigest digest = MessageDigest.getInstance("SHA-256");
if(i==a.size()-1){
byte[] hash = digest.digest(a.get(i));
byte[] dest = new byte[a.get(i).length+hash.length];
System.arraycopy(a.get(i-1), 0, dest, 0, a.get(i-1).length);
System.arraycopy(hash, 0, dest, a.get(i-1).length, hash.length);
b.add(dest);
}
else{
byte[] hash = digest.digest(b.get(0));
if(i!=0){
byte[] dest = new byte[a.get(i-1).length+hash.length];
System.arraycopy(a.get(i-1), 0, dest, 0, a.get(i-1).length);
System.arraycopy(hash, 0, dest, a.get(i-1).length, hash.length);
b.clear();
b.add(dest);
}else{
System.out.println(bytesToHex(hash));}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(inStream!=null)
inStream.close();
if(bis!=null)
bis.close();
}
}
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
我在这次尝试中直接将文件读取为 1KB 块。在此尝试中,由于某种原因,散列需要很长时间。
package com.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReadFileByteByByte2 {
public static void main(String[] args) throws Exception {
InputStream inStream = null;
BufferedInputStream bis = null;
try{
inStream = new FileInputStream("C:\\aa.mp4");
bis = new BufferedInputStream(inStream);
int numByte = bis.available();
System.out.println(numByte/1024);
ArrayList<byte[]> a = new ArrayList<>();
ArrayList<byte[]> b = new ArrayList<>();
byte[] buf = new byte[numByte];
int ii=0;
while(bis.read(buf, ii, 1024)!=-1){
a.add(buf);
}
System.out.println(a.size());
for(int i=a.size()-1,j=-1;i>=0;i--,j++){
MessageDigest digest = MessageDigest.getInstance("SHA-256");
if(i==a.size()-1){
System.out.println(a.get(i).toString());
byte[] hash = digest.digest(a.get(i));
byte[] dest = new byte[a.get(i).length+hash.length];
System.arraycopy(a.get(i-1), 0, dest, 0, a.get(i-1).length);
System.arraycopy(hash, 0, dest, a.get(i-1).length, hash.length);
b.add(dest);
}
else{
System.out.println(i);
byte[] hash = digest.digest(b.get(0));
if(i!=0){
byte[] dest = new byte[a.get(i-1).length+hash.length];
System.arraycopy(a.get(i-1), 0, dest, 0, a.get(i-1).length);
System.arraycopy(hash, 0, dest, a.get(i-1).length, hash.length);
b.clear();
b.add(dest);
}else{
System.out.println(bytesToHex(hash));}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(inStream!=null)
inStream.close();
if(bis!=null)
bis.close();
}
}
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
非常感谢任何帮助。提前致谢。
【问题讨论】:
标签: java