Return hash string by number

0

I have built a hash program that when passing a string returns a coded number, now I would like a function that would do the opposite of the same formula that we did when coding that is to say that we pass a number and show its corresponding string is To say the opposite, I do not know how to start the method I leave what I have stated.

    
asked by Rafa Alvarez-Ossorio Martin 10.05.2018 в 01:38
source

1 answer

1

It's simple.

Notice that you are building the key by multiplying and adding. Therefore, to undo the key you will have to divide by 73 and the remainder that remains will be the index of the character of the alphabet.

Obviously, doing so will have the characters of the original string in reverse order, but that is already a triviality.

I edit to add a very simple and fast implementation. As I said in the comments, you have to worry about turning the result so that the string is readable. There you have the code adapted to use BigInteger and not have problems of out of range.

public String hash2(BigInteger lo) {
    BigInteger mod = lo.mod(multiplicador);
    lo = lo.divide(multiplicador);
    String res = alfabeto.charAt(mod.intValue()) + "";

    while (lo.compareTo(tres) > 0) {
        mod = lo.mod(multiplicador);
        lo = lo.divide(multiplicador);
        res += alfabeto.charAt(mod.intValue());
    }

    return res;
}

public static void main(String[] args) {

    HashPrueba hp = new HashPrueba();
    //hp.hash2(405611771492327055239928692427624877908760706915042668827730030781521901952046793);
    System.out.println(hp.hash2(hp.hash("javaguay")));

}
    
answered by 10.05.2018 / 09:05
source