{"id":273,"date":"2020-02-20T22:12:58","date_gmt":"2020-02-20T22:12:58","guid":{"rendered":"https:\/\/codesupply.co\/et-ante-donec-rutrum-integer-amet-ligula-vel-imperdiet-eu-viverra-eget\/"},"modified":"2022-12-06T00:44:35","modified_gmt":"2022-12-06T00:44:35","slug":"how-to-store-passwords-securely-with-pbkdf2","status":"publish","type":"post","link":"https:\/\/nootherjake.com\/blog\/how-to-store-passwords-securely-with-pbkdf2\/","title":{"rendered":"How to store passwords securely with PBKDF2"},"content":{"rendered":"<p><b>What is a hash?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">To store a password securely you need to\u00a0<\/span><b>encrypt<\/b><span style=\"font-weight: 400;\"> it in some way so nobody, but the user will know their password. To do this encryption a <\/span><b>cryptographic hash function<\/b><span style=\"font-weight: 400;\"> is needed. A\u00a0<\/span><b>hash function<\/b><span style=\"font-weight: 400;\"> maps an arbitrarily long input to a fixed-length output. A <\/span><b>cryptographic hash function<\/b><span style=\"font-weight: 400;\"> has a special attribute, that it&#8217;s a one-way function, so it cannot be inverted. In other words, the original value cannot be calculated from the hashed value.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">One character difference between two strings will make the hashed strings<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">completely different, e.g:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">password1\u00a0is hashed to\u00a03ca4197139361dfa9200066fc86ad494\u00a0and<\/span><\/p>\n<p><span style=\"font-weight: 400;\">password2\u00a0is hashed to\u00a0767a9682d5a6faf085a93b700d847350<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So the password checking will look like this:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">When the user creates a new account, their password is hashed and the hash is stored, not the password itself<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">When the user logs in, their password is hashed, and the newly hashed password gets compared with the stored hash<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">There are several hash functions, but in this case do not use fast cryptographic hash functions, like SHA1, SHA256, SHA512, MD5, as they are suspect to brute-force attacks. The slower the algorithm, the longer it takes for an attacker to simply try and match all possible values. Use PBKDF2, bcrypt, scrypt or Argon2. In this tutorial you will write a hash function that<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">uses PBKDF2.<\/span><\/p>\n<p>If you are a java developer, PBKDF2 (Password-Based Key Derivation Function 2) is an excellent algorithm to use. In PBKDF2 we can force the algorithm to behave slowly by increasing its iteration count.<\/p>\n<p>This article teaches you how to hash passwords using the Java implementation of the PBKDF2 algorithm.<\/p>\n<p>In simple words, PBKDF2 is a variant of the key stretching algorithm which uses a pseudorandom function to increase the computation power of the hashing for password storage.<\/p>\n<p>Storing user passwords securely can be a challenging task. It is well known that as much as an attacker wants to gain access to a user\u2019s password, the user wants to protect their password from being stolen. Password management tools such as one time password tokens and hardware tokens do exist but they are often expensive and are generally not convenient for everyday use.<\/p>\n<p>Passwords can be the most valuable data to an attacker because stolen passwords can provide attackers the ability to bypass most of the security parameters that exist in the system.<\/p>\n<p>The PBKDF2 algorithm includes a salt and iteration count. The iteration count determines the hashing time, which is deliberately made slow to prevent brute-force attacks. The salt is an extra input to the algorithm that makes each password unique by adding a small piece of random information that doesn&#8217;t affect security but requires additional computation.<\/p>\n<p>Password hashing is a key component in web application security. A good hashing scheme will allow us to quickly authenticate a password while making it very hard (practically impossible) for a hacker to brute force the hashed value of a password. Hashing passwords is not enough though. We need to store our hashed passwords securely so that even if there is a breach attackers will only have access to useless data.<\/p>\n<p>In this tutorial, you\u2019ll learn how to properly store your passwords using the PBKDF2 algorithm.<\/p>\n<p>Passwords are the most valuable data to an attacker. When stolen, passwords can provide attackers the ability to bypass most of the security perimeters that exist in the system.<\/p>\n<p>By definition, a password is a secret word or phrase. It must be hidden from view and used by the user to protect his or her account. There are two important aspects to consider when generating and storing passwords:<\/p>\n<p>The process of storing passwords securely is known as password hashing and the tool which we use for this purpose is called a Hashing Algorithm. The PBKDF2 algorithm is one such hashing algorithm that can be used to hash passwords in a highly secure manner<\/p>\n<p>This tutorial will teach you how to use the PBKDF2 algorithm to hash passwords in a simple manner.<\/p>\n<p>Unlike all other hashing algorithms, in PBKDF2 the length of the hash is not fixed. The length of the hash can be any number between the minimum and maximum seed count specified. Increasing this number decreases the rate of successful brute force attempts by increasing the number of iterations required to create a password hash.<\/p>\n<p>By default, java security.MessageDigest provides a single-iteration PBKDF2 (PKCS5). One can increase this iteration count for improved security.<\/p>\n<p>In the modern era of computing, passwords are important. When it comes to security, passwords are the most critical element of any system. If an attacker steals your password, they can bypass most security parameters and gain access to your data.<\/p>\n<p>In this article, I will explain how to securely store passwords in a database by using an example.<\/p>\n<p><b>Demonstration challenge<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Your hash function has 4 arguments: password, salt, iteration, key length.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>password:<\/b><span style=\"font-weight: 400;\">\u00a0it is the user&#8217;s password that will be hashed<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>salt:<\/b><span style=\"font-weight: 400;\"> a random string that mixes up a password&#8217;s hash, so if two users have the same password the two hashes won&#8217;t be the same<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>iteration:<\/b><span style=\"font-weight: 400;\"> the number of iterations, it determinates how slow the hash function will be, but be careful if the number of iterations is too high, it will use too many resources on the server. It should be around 10000 &#8211; 100000<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>key length:<\/b><span style=\"font-weight: 400;\"> required output length of the hash function. 256 or 512 key length is safe<\/span><\/li>\n<\/ul>\n<p><b>Step 0:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">You need to check whether the password argument is empty:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public static String hash(String password, byte[] salt, int iteration, int keylength) throws Exception{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if (password == null || password.length() == 0)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw new IllegalArgumentException(&#8220;Empty password&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4368 size-full\" src=\"https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704.png\" alt=\"\" width=\"2074\" height=\"1554\" srcset=\"https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704.png 2074w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-300x225.png 300w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-1024x767.png 1024w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-768x575.png 768w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-1536x1151.png 1536w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-2048x1535.png 2048w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-200x150.png 200w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-260x195.png 260w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-380x285.png 380w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-800x599.png 800w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-1-e1670287258704-1160x869.png 1160w\" sizes=\"auto, (max-width: 2074px) 100vw, 2074px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4369 size-full\" src=\"https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851.png\" alt=\"\" width=\"2091\" height=\"1503\" srcset=\"https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851.png 2091w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851-300x216.png 300w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851-1024x736.png 1024w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851-768x552.png 768w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851-1536x1104.png 1536w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851-2048x1472.png 2048w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851-380x273.png 380w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851-800x575.png 800w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-md5-sha-1-sha23-e1670287308851-1160x834.png 1160w\" sizes=\"auto, (max-width: 2091px) 100vw, 2091px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4370 size-full\" src=\"https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744.png\" alt=\"\" width=\"2049\" height=\"1570\" srcset=\"https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744.png 2049w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744-300x230.png 300w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744-1024x785.png 1024w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744-768x588.png 768w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744-1536x1177.png 1536w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744-380x291.png 380w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744-800x613.png 800w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-md5-e1670287391744-1160x889.png 1160w\" sizes=\"auto, (max-width: 2049px) 100vw, 2049px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4371 size-full\" src=\"https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450.png\" alt=\"\" width=\"2730\" height=\"1537\" srcset=\"https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450.png 2730w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450-300x169.png 300w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450-1024x577.png 1024w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450-768x432.png 768w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450-1536x865.png 1536w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450-2048x1153.png 2048w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450-380x214.png 380w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450-800x450.png 800w, https:\/\/nootherjake.com\/blog\/wp-content\/uploads\/2020\/02\/hashing-salting-How-to-store-passwords-securely-PBKDF2-sha-e1670287430450-1160x653.png 1160w\" sizes=\"auto, (max-width: 2730px) 100vw, 2730px\" \/><\/p>\n<p><b>Step 1:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Instantiate a SecretKeyFactory object that converts the secret keys of <\/span><span style=\"font-weight: 400;\">the specified algorithm, in our tutorial it is <\/span><b>PBKDF2WithHmacSHA256 <\/b><\/p>\n<p><span style=\"font-weight: 400;\">public static String hash(String password, byte[] salt, int iteration, int keylength) throws Exception{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if (password == null || password.length() == 0)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw new IllegalArgumentException(&#8220;Empty password&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0SecretKeyFactory f = SecretKeyFactory.getInstance(&#8220;PBKDF2WithHmacSHA256&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><b>Step 2:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Generate a SecretKey object from the provided key specification<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public static String hash(String password, byte[] salt, int iteration, int keylength) throws Exception{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if (password == null || password.length() == 0)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw new IllegalArgumentException(&#8220;Empty password&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0SecretKeyFactory f = SecretKeyFactory.getInstance(&#8220;PBKDF2WithHmacSHA256&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0SecretKey key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt.getBytes(), iteration, keylength));<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><b>Step 3:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Return with the generated hash. It is a byte[], so you need to convert it to String type as the following code shows:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">public static String hash(String password, String salt, int iteration, int keylength) throws Exception{<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0if (password == null || password.length() == 0)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw new IllegalArgumentException(&#8220;Empty password&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0SecretKeyFactory f = SecretKeyFactory.getInstance(&#8220;PBKDF2WithHmacSHA256&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0SecretKey key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt.getBytes(), iteration, keylength));<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return Base64.getEncoder().encodeToString(key.getEncoded());<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<blockquote>\n<div>\n<div>package hashingfunction;<\/div>\n<div>import java.security.SecureRandom;<\/div>\n<div>import java.util.Base64;<\/div>\n<div>import java.util.Scanner;<\/div>\n<div>import javax.crypto.SecretKey;<\/div>\n<div>import javax.crypto.SecretKeyFactory;<\/div>\n<div>import javax.crypto.spec.PBEKeySpec;<\/div>\n<div>\/**<\/div>\n<div>* Cryptogenic Hashing function:<\/div>\n<div>*<\/div>\n<div>* In this class we will use PBKDF2,<\/div>\n<div>* to encrypt users password<\/div>\n<div>*<\/div>\n<div>* This function will be intentionally made slow, to make it withstand brute<\/div>\n<div>* force attacks.<\/div>\n<div>*<\/div>\n<div>* @author Jake Adebayo<\/div>\n<div>*\/<\/div>\n<div>public class HashingFunction {<\/div>\n<div>\/**<\/div>\n<div>* @param password: it is the user&#8217;s password that will be hashed<\/div>\n<div>* @param salt: a random string that mixes up a password&#8217;s hash,<\/div>\n<div>* so if two users has the same password the two hashes won&#8217;t be the same<\/div>\n<div>* @param iteration: the number of iterations, it determinates how slow the hash function will be,<\/div>\n<div>* but be careful if the number of iterations is too high,<\/div>\n<div>* it will use too much resources on the server. It should be around 10000 &#8211; 100000<\/div>\n<div>* @param keylength: required output length of the hash function. 256 or 512 keyLength is safe<\/div>\n<div>*<\/div>\n<div>* @return key<\/div>\n<div>* @throws Exception<\/div>\n<div>*\/<\/div>\n<div>publicstaticStringhash(Stringpassword, byte[] salt, intiteration, intkeylength) throwsException {<\/div>\n<div>StringhashedPassword = &#8220;&#8221;;<\/div>\n<div>if (password == null || password.length() == 0)<\/div>\n<div>thrownewIllegalArgumentException(&#8220;Empty password&#8221;);<\/div>\n<div>if (iteration &gt; 100000 || iteration &lt; 10000)<\/div>\n<div>thrownewIllegalArgumentException(&#8220;Iteration should be in range (10000-100000)&#8221;);<\/div>\n<div>if (keylength != 256 &amp;&amp; keylength != 512)<\/div>\n<div>thrownewIllegalArgumentException(&#8220;Keylength is not safe&#8221;);<\/div>\n<div>SecretKeyFactoryf = SecretKeyFactory.getInstance(&#8220;PBKDF2WithHmacSHA256&#8221;);<\/div>\n<div>SecretKeykey = f.generateSecret(newPBEKeySpec(password.toCharArray(), salt, iteration, keylength));<\/div>\n<div>returnBase64.getEncoder().encodeToString(key.getEncoded());<\/div>\n<div>}<\/div>\n<div>\/**<\/div>\n<div>* Creating random salt to prevent attacks,<\/div>\n<div>* from rainbow table<\/div>\n<div>* @return salt<\/div>\n<div>*\/<\/div>\n<div>publicstaticbyte[] randomSalt() {<\/div>\n<div>byte[] salt = newbyte[16];<\/div>\n<div>SecureRandomsecure_random = newSecureRandom();<\/div>\n<div>secure_random.nextBytes(salt);<\/div>\n<div>return salt;<\/div>\n<div>}<\/div>\n<div>\/**<\/div>\n<div>* Use this method if you don&#8217;t want to use<\/div>\n<div>* randomSalt()<\/div>\n<div>*<\/div>\n<div>* @param salt<\/div>\n<div>* @return<\/div>\n<div>*\/<\/div>\n<div>publicstaticbyte[] getSalt(Stringsalt) {<\/div>\n<div>returnsalt.getBytes();<\/div>\n<div>}<\/div>\n<div>\/\/ checks if entered password matches with hashed password<\/div>\n<div>publicstaticvoidcheckPassword(Stringpassword, StringhashedPassword, byte[] salt, intiteration, intkeylength) throwsException {<\/div>\n<div>if (hashedPassword.equals(hash(password, salt, iteration, keylength)))<\/div>\n<div>System.out.println(&#8220;Password Matched!&#8221;);<\/div>\n<div>else<\/div>\n<div>System.out.println(&#8220;Password Didn&#8217;t Match!&#8221;);<\/div>\n<div>}<\/div>\n<div>\/**<\/div>\n<div>* @param args the command line arguments<\/div>\n<div>* @throws java.lang.Exception<\/div>\n<div>*\/<\/div>\n<div>publicstaticvoidmain(String[] args) throwsException {<\/div>\n<div>finalintITERATION = 15000;<\/div>\n<div>finalintKEY_LENGTH = 256;<\/div>\n<div>Stringpassword = &#8220;jakespassword1234&#8221;;<\/div>\n<div>byte[] randSalt = randomSalt();<\/div>\n<div>StringhashedKey = hash(password, randSalt, ITERATION, KEY_LENGTH);<\/div>\n<div>System.out.println(&#8220;Your password is hashed to : &#8221; + hashedKey);<\/div>\n<div>Scannerscanner = newScanner(System.in);<\/div>\n<div>System.out.println(&#8220;Enter your password : &#8220;);<\/div>\n<div>StringenterPassword = scanner.next();<\/div>\n<div>checkPassword(enterPassword, hashedKey, randSalt, ITERATION, KEY_LENGTH);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"How to store passwords securely with PBKDF2\n","protected":false},"author":1,"featured_media":4367,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"gallery","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"csco_singular_sidebar":"default","csco_page_header_type":"large","csco_page_load_nextpost":"default","csco_post_video_location":[],"csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0,"footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-273","1":"post","2":"type-post","3":"status-publish","4":"format-gallery","5":"has-post-thumbnail","7":"category-cloud-security","8":"post_format-post-format-gallery","9":"cs-entry","10":"cs-video-wrap"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":4,"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"predecessor-version":[{"id":4373,"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/posts\/273\/revisions\/4373"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/media\/4367"}],"wp:attachment":[{"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nootherjake.com\/blog\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}