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:
- always the same assuming same String input
- as close as possible to true-uniqueness i.e. avoiding as much as possible different strings resulting same hash string.
- 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 "";
}