Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compute md5 hash of multi part data (multiple strings)

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?

like image 201
Fakrudeen Avatar asked Jan 24 '11 17:01

Fakrudeen


People also ask

How do you generate the MD5 hash of a string?

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.

Can two strings have same MD5?

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).

What is MD5 hash generator?

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.

Can two values have the same 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.


1 Answers

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.

like image 167
Sebastian Paaske Tørholm Avatar answered Sep 20 '22 10:09

Sebastian Paaske Tørholm