Creating an encryption key from several other keys and using hash functions I want to combine two or more keys to create a single encryption key that relies on all of them.  What is the proper method for doing that? Simple XOR? Using hash functions?  Something else?

I personally used this: k = md5( key1 || key2 ).  Note: || means concatenation.  I used md5 because I use 128-bit encryption and thus need a 128 bit output key.

Some other questions arise here for me:


Is using MD5 secure for this specific purpose?
I don't know of any other standard 128-bit output hash functions. It seems newer cryptographic hash functions all have 256-bit and more output lengths.
So is using another hash function that has a 256/512-bit output and then truncating the result down to 128 bits secure to an equal or more degree than using MD5?


Note that key1 and key2 are random keys, not passwords, and thus key stretching is not relevant or applicable.

If it is relevant, I generated both keys, and I know that both keys are cryptographically random.  Neither key was supplied by an untrusted party.  At present both keys happen to be the same length, but I'd prefer a more general solution that does not rely upon this assumption, if possible.
If the keys have constant, known length, I'd concatenate them, and then apply SHA256. If they have variable length, applying some separation mechanism might be useful.

Truncating hash functions works well. If the original hash function is good, a truncated hash function has the same properties, albeit at a correspondingly lower security level. Truncating SHA-256 is certainly better than using MD5.

I recommend something like:

Truncate(SHA-256(output-size || number-of-keys || sizeof(key1) || key1 || sizeof(key2) || key2 ...), output-size) where output-size &lt;= 256