MoienGK
MoienGK

Reputation: 4654

Is it safe (in matter of uniqueness) to use UUID to generate a unique identifier for specific string?

String myText;
UUID.nameUUIDFromBytes((myText).getBytes()).toString();

I am using above code to generate a representative for specific texts. For example 'Moien' should always be represeted with "e9cad067-56f3-3ea9-98d2-26e25778c48f", not any changes like project rebuild should be able to change that UUID.

The reason why I'm doing this is so that I don't want those specific texts to be readable(understandable) to human.

Note: I don't need the ability to regenerate the main text (e.g "Moien") after hashing .

I have an alternative way too :

            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest((matcher.group(1)).getBytes("UTF-8"));
            String a = Base64.encode(hash);

Which od you think is better for my problem?

Upvotes: 5

Views: 4837

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499800

UUID.nameUUIDFromBytes appears to basically just be MD5 hashing, with the result being represented as a UUID.

It feels clearer to me to use a base64-encoded hash explicitly, partly as you can then control which hash gets used - which could be relevant if collisions pose any sort of security risk. (SHA-256 is likely a better option than MD5 for exactly that reason.) The string will be longer from SHA-256 of course, but hopefully that's not a problem.

Note that in either case, I'd convert the string to text using a fixed encoding via StandardCharsets. Don't use the platform default (as per your first snippet) and prefer StandardCharsets over magic string values (as per your second snippet).

Upvotes: 5

Related Questions