# How to use HKDF to combine two keys

I have heard that to combine two keys HKDF is better than using XOR function. But if you look at the HKDF RFC I could not figure out how to use it for concatenating two keys. What I can see is HKDF can be used to derive a key from another key. So I want to know how to use HKDF to concatenate two keys.

-

## 2 Answers

If you have two keys of constant length, using HKDF is rather pointless.

You can simply combine them using a hash function like SHA-2:

$$combined\_key = \text{SHA-2}(k_1 || k_2)$$

-
In CSE I have seen HKDF is recommended to use for key concatenation because it preserves the entropy unlike in a XOR function. crypto.stackexchange.com/questions/11405/…;. So why you are saying HKDF is pointless. Please enlighten me. I want the finale key to be as random as possible. Thats why I am using HKDF to concatenation without using HASH alto or a XOR function. – user10988 Dec 20 '13 at 7:16
@user10988 In your scenario HKDF basically comes down to a HMAC-SHA-256 call. HMAC has no advantage compared to SHA in this scenario, because the input is of constant length, so you might as well hash. – orlp Dec 20 '13 at 7:32
Thank you for explanation. One more question.Will using a hash function be better than use XOR the two keys(Considering the entropy)? – user10988 Dec 20 '13 at 7:52
@user10988 Hashing is better than xor-ing, for the same reason that HKDF is better than xor-ing. HKDF with a constant salt is a hash. – CodesInChaos Dec 20 '13 at 9:00

Forgive me for sounding condescending – that’s not the intent – but to “concatenate 2 keys” you use “concatenation” operation. Not XOR, HKDF, HASH/SHA-2, etc. The only proper way of concatenating an n-bit Key1 and m-bit Key2 - without entropy loss - is to concatenate them.

-