比较两个哈希字符串并分发给Java中的元素

时间:2019-05-13 10:02:48

标签: java string hash comparison

我正在使用SHA-1哈希一些字符串(ID),并且将它们与其他哈希(也使用SHA-1)字符串(称为元素)进行比较,并且我想将ID分配给元素。我想用语句(hashedId

SHA-1方法

public static String simpleHashFunc(String a) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] messageDigest = md.digest(a.getBytes());
            BigInteger no = new BigInteger(1, messageDigest);

            String hashtext = no.toString(16);

            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }

            return hashtext;

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

测试线

 int counter1 = 0, counter2 = 0, counter3 = 0, counter4 =0;
        for (int i = 0; i < topics.size(); i++) {
            String tempHash = Broker.simpleHashFunc(topics.get(i).getBusLine().getBuslineId());
            if (tempHash.compareTo(hashedBrokers.get(0)) == -1) {
                counter1++;
                //Distribution to element1
            } else if (tempHash.compareTo(hashedBrokers.get(1)) == -1) {
                counter2++;
                //Distribution to element2
            } else if (tempHash.compareTo(hashedBrokers.get(2)) == -1) {
                counter3++;
                //Distribution to element3
            } else {
                System.out.println("Cant go to broker: " + topics.get(i).getBusLine().getBuslineId());
                counter4++;
            }
        }
        System.out.println(counter1 + "  " + counter2 + "  " + counter3 + "  " + counter4);
//The majority of IDs go to to else { } and the counter4 has the greatest number.

预期结果:

在37个ID中:20个到element1,10个到element2,7个到element3

预期结果:

在37个ID中:25到element1,10到element2,2到element3 ....

实际结果:

在37个ID中:5个到element1、10个到element2、2个到element3、20个ID不能进入元素

1 个答案:

答案 0 :(得分:1)

我们可以转到else语句,然后从mod函数ids%elements.size()开始做起。 Mod可以为0,1,2。我们使用BigInteger类来提高准确性

//hashing buslineIDs and distributing to correct Brokers
            int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0;
            for (int i = 0; i < topics.size(); i++) {
                String tempHash = Broker.simpleHashFunc(topics.get(i).getBusLine().getBuslineId());
                if (tempHash.compareTo(hashedBrokers.get(0)) == -1) {
                    counter1++;
                    //add responsibility line
                } else if (tempHash.compareTo(hashedBrokers.get(1)) == -1) {
                    counter2++;
                } else if (tempHash.compareTo(hashedBrokers.get(2)) == -1) {
                    counter3++;
                } else {
                    counter4++;
                    BigInteger tempHashBig = new BigInteger(tempHash, 32);
                    //System.out.println(tempHashBig);
                    if (tempHashBig.mod(BigInteger.valueOf(hashedBrokers.size())).equals(BigInteger.valueOf(0))) {
                        counter1++;
                    } else if (tempHashBig.mod(BigInteger.valueOf(hashedBrokers.size())).equals(BigInteger.valueOf(1))) {
                        counter2++;
                    } else if (tempHashBig.mod(BigInteger.valueOf(hashedBrokers.size())).equals(BigInteger.valueOf(2))) {
                        counter3++;
                    }

                }


            }