0

I am looking for a conversion function in android from a string of 1-20 chars in any language (e.g. Hebrew, English, Chinese) to about 20 chars e.g. from hello world to HGWSIg2YYYqZ12OrgUjk

The hash result should be:

  1. always the same assuming same String input
  2. as close as possible to true-uniqueness i.e. avoiding as much as possible different strings resulting same hash string.
  3. as fast as possible (since will be used a lot while making the UI)

The purpose is to convert a description field in firestore database to its document id, thus preventing duplicates of description automatically, with minimum overhead

I am currently using the next code which seems to work, but i am not sure that is the best approach

public static String getHash(String input)
{
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(input.getBytes());
        byte[] messageDigest = digest.digest();

        // Create Hex String
        StringBuilder hexString = new StringBuilder();
        for (byte b : messageDigest) hexString.append( Integer.toHexString( 0xFF & b ) );
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return "";
}

The source code is from here

epic
  • 1,333
  • 1
  • 13
  • 27
  • 1
    Why do you want a hash? Storing the string sounds much easier and faster. – Mat Mar 14 '20 at 10:35
  • you haven't asked a question yet, what have you tried and what are you struggling with ? – a_local_nobody Mar 14 '20 at 10:37
  • @a_local_nobody not sure why it was unclear but i modified my question – epic Mar 14 '20 at 11:40
  • "not sure why it was unclear" even if we understand entirely what you want to do, that doesn't help narrow the question down into what you've done and what you're struggling with :) – a_local_nobody Mar 14 '20 at 12:11

1 Answers1

0

If you want to add each and every char in Hashset of strings to avoid duplicate chars, as I understood:

String chars="HGWSIg2YYYqZ12OrgUjk"
Hashset<String> string=new Hashset<String>();

for(i=0;i<chars.length,i++){

string.add(chars.charAt(i));
}

when it finished you will have Hashset contains each char as element and prevent duplicates