I am trying to create a [single] md5 hash of multiple strings [in Java]. That is I want
md5(string1, string2, string3, ..., stringN)
Currently I am trying to concatenate all strings with some rarely used separator like #. That is
md5(string1#string2#...#stringN)
This looks hacky and I am worried about some weird string actually having the separator as part of it. What is the best way to do it?
Call MessageDigest. getInstance("MD5") to get a MD5 instance of MessageDigest you can use. The compute the hash by doing one of: Feed the entire input as a byte[] and calculate the hash in one operation with md.
Generally, two files can have the same md5 hash only if their contents are exactly the same. Even a single bit of variation will generate a completely different hash value. There is one caveat, though: An md5 sum is 128 bits (16 bytes).
The MD5 hashing algorithm uses a complex mathematical formula to create a hash. It converts data into blocks of specific sizes and manipulates that data a number of times. While this is happening, the algorithm adds a unique value into the calculation and converts the result into a small signature or hash.
In computer science, a hash collision or clash is when two pieces of data in a hash table share the same hash value. The hash value in this case is derived from a hash function which takes a data input and returns a fixed length of bits.
This could possibly be better:
md5(md5(string1) + md5(string2) + ... + md5(stringN))
It'd eliminate the separator problem, but it's hard to say how good it is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With