0

我目前正在研究用 Java 实现彩虹表的简单实现。并且一直在编写链查找方法时遇到问题。这是我当前的代码:

private static HashMap<Long, HashMap<Boolean, String>> chainLookup(long hashVal, HashMap<String, String> rainbowTable){
        HashMap<Long, HashMap<Boolean, String>> toRet = new HashMap<Long, HashMap<Boolean, String>>();
        HashMap<Boolean, String> toRetInternal = new HashMap<Boolean, String>();

        /*
        1. get a list of all end values in given rainbow table
        2. keep applying reduction method on the hash value and comparing to end values
        3. if reduced value in end value array, find the index and look through the corresponding table
        */

        long temp = hashVal;
        ArrayList<String> endVals = new ArrayList<String>(rainbowTable.values());
        int indexOfTable = -1;

        String reducedVal;

        for(int i=0; i<10000;i++){
            reducedVal = reductionFunction(temp, i);
            temp = hashFunction(reducedVal);

            if(endVals.contains(reducedVal)){
                System.out.println("In if");
                indexOfTable = endVals.indexOf(reducedVal);
                break;
            }
        }
        System.out.println(indexOfTable);

        if(indexOfTable==-1){
            toRetInternal.put(false, "null");
            toRet.put(hashVal, toRetInternal);
            return toRet;
        }else{
            String start = (String) rainbowTable.keySet().toArray()[indexOfTable];
            String temp1 = start;
            long hashedTemp;

            for (int j=0; j<10000;j++){
                hashedTemp = hashFunction(temp1);
                if (hashedTemp == hashVal){
                    toRetInternal.put(true, temp1);
                    toRet.put(hashVal, toRetInternal);
                    return toRet;
                }else{
                    temp1 = reductionFunction(hashedTemp, j);
                }
            }
        }
        toRetInternal.put(false, "null");
        toRet.put(hashVal, toRetInternal);
        return toRet;
    }

但是,对于我知道可以使用我硬编码到彩虹表中的链来破解的哈希值,第一个循环似乎无法找到它们。因此 indexOfTable 保持 -1。我不知道为什么会这样,因为代码很简单。将不胜感激任何帮助。

4

0 回答 0