如果我运行此代码,我会遇到问题,在我尝试访问此方法返回的值之前一切正常。它应该生成随机字节散列。如果我在大 for 循环(带有 j 计数器的循环)中,它工作正常,但是一旦它出来,arraylist 中的所有值都是在那里添加的最后一个值。那就是 Rainbow.get(0).start 和 rainbow.get(1).start 所在的 system.out.prints。
在底部也是 Chain 类。
这哪里搞砸了?
public static ArrayList<Chain> genRainBow(){
byte[] constant = new byte[8];
ArrayList<byte[]> hashRed = new ArrayList<byte[]>();
ArrayList<byte[]> variations = new ArrayList<byte[]>();
ArrayList<Chain> rnbow = new ArrayList<Chain>();
//rainbow = new ArrayList<Chain>();
//byte[] start;
byte[] hash = null;
int collision = 0;
int allColl = 0;
Random rnd = new Random();
byte[] randomBytes = new byte[8];
byte[] red;
byte[] copyHash = null;
for(int j = 0; j < 1000; j++){
hashRed = new ArrayList<byte[]>();
collision = 0;
rnd.nextBytes(randomBytes);
red = truncate12(randomBytes);
byte[] start = red;
for(int i = 0; i < 50; i++){
//System.out.println(i + " " + DatatypeConverter.printHexBinary(red));
hash = Main.DES(red, constant);
//System.out.println( i + " " +DatatypeConverter.printHexBinary(hash));
red = Main.reductionChain(hash, i);
if(containsList(hashRed, hash))
collision++;
hashRed.add(hash);
//rainbow.put(red, hash);
}
hash = red;
rnbow.add(new Chain(start, hash));
// rainbow.add(new Chain(start, hash));
System.out.println("STR" + DatatypeConverter.printHexBinary(rainbow.get(j).start));
if(!containsList(variations, hash))
variations.add(hash);
System.out.println("Collision: " + collision);
System.out.println();
allColl += collision;
}
System.out.println("STR" + DatatypeConverter.printHexBinary(rainbow.get(0).start));
System.out.println("STR" + DatatypeConverter.printHexBinary(rainbow.get(1).start));
System.out.println("variations: " + variations.size());
System.out.println("all collisions: " + allColl);
System.out.println();
return rnbow;
}
package rnbow;
public class Chain {
byte[] start;
byte[] end;
public Chain(byte[] st, byte[] en){
start = st;
end = en;
}
public byte[] printStart(){
return start;
}
}