Password - laminas-crypt - Laminas Docs (2024)

On this page
  • Bcrypt
  • Apache

Reference

In the Laminas\Crypt\Password namespace you will find a number of passwordformats supported by the laminas-crypt component. These currently include:

  • bcrypt
  • Apache (htpasswd)

If you need to choose a password format to store a user’s password, we suggestusing the bcrypt algorithm, as it is considered secure against brute forcingattacks (see details below).

Bcrypt

The bcrypt algorithm is a hashing algorithmthat is widely used and recommended by the security community to store userpasswords in a secure way.

Classic hashing mechanisms like MD5 or SHA, with or without a salt value, arenot considered secure anymore (read this post to understandwhy).

The security of bcrypt is related to the speed of the algorithm. Bcrypt is veryslow, and can take up to a second to generate a hash value. That means a bruteforce attack is impossible to execute, due to the amount of time that required.

Bcrypt uses a cost parameter that specify the number of cycles to use in thealgorithm. Increasing this number the algorithm will spend more time to generatethe hash output. The cost parameter is represented by an integer value between 4to 31. The default cost value of Laminas\Crypt\Password\Bcrypt is 10, requiringaround 0.07s using a CPU Intel i5 at 3.3Ghz (the cost parameter is a relativevalue according to the speed of the CPU used). Starting with version 2.3.0, wechanged the default value of the cost parameter from 14 to 10, in an effort toreduce denial-of-service attacks due to too high computational timerequirements. (Read this article on aggressive password stretchingfor more information).

If you want to change the cost parameter of the bcrypt algorithm, you can usethe setCost() method. Please note, if you change the cost parameter, theresulting hash will be different. However, This will not affect the verificationprocess of the algorithm, therefore not breaking the password hashes you alreadyhave stored; Bcrypt reads the cost parameter from the hash value during passwordauthentication. All of the parts needed to verify the hash are present in thehash itself,, separated with $’s; first the algorithm, then the cost, thesalt, and then finally the hash.

The example below demonstrates using the bcrypt algorithm to store a user’s password:

use Laminas\Crypt\Password\Bcrypt;$bcrypt = new Bcrypt();$securePass = $bcrypt->create('user password');

The output of the create() method is the hash of the password. This value canthen be stored in a repository like a database (the output is a string of 60bytes).

Bcrypt truncates Input > 72 bytes

The input string of the bcrypt algorithm is limited to 72 bytes. If you use astring with a length more than this limit, bcrypt will consider only the first72 bytes. If you need to use a longer string, you should pre-hash it.We provide the class Laminas\Crypt\Password\BcryptSha for performingpassword pre-hashing of hash input > 72 bytes.

To verify if a given password is valid against a bcrypt value you can use theverify() method. The example below demonstrates verification:

use Laminas\Crypt\Password\Bcrypt;$bcrypt = new Bcrypt();$securePass = 'the stored bcrypt value';$password = 'the password to check';if ($bcrypt->verify($password, $securePass)) { echo "The password is correct! \n";} else { echo "The password is NOT correct.\n";}

Bcrypt also uses a salt value to improve the randomness of the algorithm.By default, Laminas\Crypt\Password\Bcrypt generates a random salt foreach hash. If you want to specify a preselected salt you can use the setSalt()method.

We also provide a getSalt() method to retrieve the salt specified by the user.The salt and the cost parameter can be also specified during the constructor ofthe class, as demonstrated below:

use Laminas\Crypt\Password\Bcrypt;$bcrypt = new Bcrypt([ 'salt' => 'random value', 'cost' => 11]);

Version 3.0

Starting with version 3.0, we now use the password_hash()and `password_verify() functions introducedin PHP 5.5 to generate bcrypt hash values. We provide backwardscompatibility tests to ensure that any hashes generated with version 2releases can still be validated under version 3.

Apache

Laminas\Crypt\Password\Apache supports all the password formats used byApache(htpasswd). These formats include:

  • CRYPT, which uses the traditional Unix crypt(3) function with a randomly-generated 32-bit salt (only 12 bits used) and the first 8 characters of the password;
  • SHA1, “{SHA}” + Base64-encoded SHA-1 digest of the password;
  • MD5, “$apr1$” + the result of an Apache-specific algorithm using an iterated (1,000 times) MD5 digest of various combinations of a random 32-bit salt and the password.
  • Digest, the MD5 hash of the string user:realm:password as a 32-character string of hexadecimal digits. realm is the Authorization Realm argument to the AuthName directive in httpd.conf.

In order to specify the format of the Apache’s password, use the setFormat()method. An example with all the formats usage is demonstrated below:

use Laminas\Crypt\Password\Apache;$apache = new Apache();$apache->setFormat('crypt');printf ("CRYPT output: %s\n", $apache->create('password'));$apache->setFormat('sha1');printf ("SHA1 output: %s\n", $apache->create('password'));$apache->setFormat('md5');printf ("MD5 output: %s\n", $apache->create('password'));$apache->setFormat('digest');$apache->setUserName('enrico');$apache->setAuthName('test');printf ("Digest output: %s\n", $apache->create('password'));

You can also specify the format of the password during the constructor of the class:

use Laminas\Crypt\Password\Apache;$apache = new Apache([ 'format' => 'md5']);

Other possible parameters to pass in the constructor are username and authname,for the digest format.

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!

Password - laminas-crypt - Laminas Docs (2024)

FAQs

What is the format of crypt password? ›

The crypt function uses only the first eight characters from the password string; the user's password is truncated to eight characters. If the password contains less than eight characters, it is padded with zero bits on the right. The 56-bit DES key is derived by using the 7 bits from each character.

What is Bcrypt's cost? ›

Bcrypt uses a cost parameter that specify the number of cycles to use in the algorithm. Increasing this number the algorithm will spend more time to generate the hash output. The cost parameter is represented by an integer value between 4 to 31.

What is the correct password format? ›

Use a mix of alphabetical and numeric, a mixture of upper and lowercase, and special characters when creating your unique passphrase. Use unique passwords or passphrases: You should have a unique password for each of your accounts. This way, if one of your accounts is compromised, your other accounts remain secure.

How do I open a crypt file? ›

How do you open CRYPT files? You need a suitable software like WhatsApp Messenger from WhatsApp to open a CRYPT file. Without proper software you will receive a Windows message "How do you want to open this file?" or "Windows cannot open this file" or a similar Mac/iPhone/Android alert.

What is the difference between crypt and bcrypt? ›

crypt without arguments is the standard UNIX password hashing function ported to PHP whereas bcrypt is based on the Blowfish algorithm. Bcrypt was designed specifically for hashing passwords. Read the documentation: password_hash() uses strong hash, generates strong salt, applies proper rounds automatically.

Is bcrypt still secure? ›

The takeaway is this: bcrypt is a secure algorithm but remember that it caps passwords at 72 bytes. You can either check if the passwords are the proper size, or opt to switch to argon2, where you'll have to set a password size limit.

What is bcrypt used for? ›

Bcrypt is a valuable tool to use to hash and store passwords. Its major benefits include: Slow runtime. Bcrypt is a slow-functioning algorithm that takes time to create password hashes and requires time to decrypt them, significantly slowing hacker attempts to break the bcrypt hash.

What is the format of a password? ›

Create strong passwords

At least 12 characters long but 14 or more is better. A combination of uppercase letters, lowercase letters, numbers, and symbols. Not a word that can be found in a dictionary or the name of a person, character, product, or organization. Significantly different from your previous passwords.

What is the password hash $6$ format? ›

If a password hash starts with "$6$", it is likely a SHA-512 hash, which is a Unix variant [1].

What is passwd format? ›

The /etc/passwd file is a colon-separated file that contains the following information: User name. Encrypted password. User ID number (UID)

What is a cipher password? ›

Password encryption is a method for securing data that is stored or transferred by transforming it into an unintelligible form called a ciphertext. This ciphertext is then linked to a decryption key that enables the information to be converted back into its original form.

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6152

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.