A , or KDF, derives one or many secret keys from a secret value. Therefore, If you've ever needed to store a password in a database or create a private key from a password, you may have used a KDF. key derivation function For instance, some examples of popular KDFs: Argon2 Scrypt PBKDF2 Key derivation functions are a must-have for most web applications. The number of sites that use plain-text or weak-hash password storage is frightening. If a site ever emails you a copy of your password... RUN! Are KDFs just hash functions? No, but there is overlap. In order to understand KDFs, let's first go through a quick refresher on hash functions. Some hash functions for example: SHA-256 MD5 A hash function takes an input and creates an output. In most password hashing scenarios it looks something like this: sha256( ) -> ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f "password123" The function must have the following properties: It scrambles data deterministically (Same input, same output) No matter the input, the output of a hash function always has the same size It cannot retrieve the input from the output (one-way function) So what is the difference? There are different types of KDFs. Some are based on stream or block ciphers, but in this article, we will focus on the most common type, . hash-based key derivation functions As it turns out, all hash-based KDFs are secure hash functions, but not all hash functions are hashed based KDFs. In addition to the properties of a hash function, KDFs can serve the : following purposes Key Stretching Key Whitening Key Separation Key Strengthening Let's look at each case separately, with the following definition of our general KDF in mind: derivedKey = keyDerivationFunction(originalKey, salt, difficulty) is random data used to protect against pre-computation attacks or rainbow tables. Salt can be used to make the KDF slower via intense computation, memory, or parallelism requirements. This protects against brute force attacks because it will take an attacker longer per guess. Difficulty Key Stretching Key stretching is the most common use case for the average developer. The idea is to take a key with low-entropy (security or randomness) and stretch it into a longer key that is more secure. Passwords are undoubtedly a great example. For example, many websites use Bcrypt to stretch keys: = bcrypt(password, salt, difficulty) passwordForDB Key Separation KDFs allow child keys to be created from a master key. This can be used in applications like Bitcoin where child keys can control sections of a wallet. However, only the master has full control. This is done through the use of different salts. For example: = kdf(masterKey, saltOne, difficulty) = kdf(masterKey, saltTwo, difficulty) = kdf(masterKey, saltThree, difficulty) childOne childTwo childThree Key Strengthening Strengthing extends a key with a random salt, but then so it can't be used again. This makes the resulting key stronger without adding significant vulnerabilities to the system. deletes the salt Should I Use KDFs? Yes. Most often when storing passwords in databases, but also if any of these other use cases fall into the domain of your code. Tweet at me if you have comments or questions. To read more check out the . HKDF paper Lane on Twitter: @wagslane Lane on Dev.to: wagslane This post originally appeared on Qvault: https://qvault.io/2019/12/30/very-basic-intro-to-key-derivation-functions-argon2-scrypt-etc/