Search code examples
javastringhashcomparison

Compare two hashed Strings and distribute to elements in Java


I'm hashing with SHA-1 some Strings (IDs) and i'm comparing them with other hashed (also with SHA-1) Strings (which are referred to elements), and i want to distribute the IDs to the elements. I want to distribute them with the statement (hashedId < hashedStringElement1, 2 or 3). For test purposes as you can see below i put counters and an error print if the hashedID cant go to an Element. Every hashedID must go to Element1, 2 or 3. For correct distribution i think i could use the MOD function but i don't know how. It's ok if the IDs are not distributed equally but they have to go somewhere.

SHA-1 method

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);
        }
    }

Testing Lines

 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.

Expected results:

Out of 37IDs: 20 to element1, 10 to element2, 7 to element3

Expected results:

Out of 37IDs: 25 to element1, 10 to element2, 2 to element3 ....

Actual results:

Out of 37IDs: 5 to element1, 10 to element2, 2 to element3, 20 ids cant go to element


Solution

  • We can go to else statement and then do it from the begining with the mod function ids % elements.size(). Mod can be 0,1,2. We use the BigInteger class for accuracy

    //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++;
                        }
    
                    }
    
    
                }