Compatibility with the password_* functions that ship with PHP 5.5

Overview

password_compat

Build Status Code Climate

This library is intended to provide forward compatibility with the password_* functions that ship with PHP 5.5.

See the RFC for more detailed information.

Requirements

This library requires PHP >= 5.3.7 OR a version that has the $2y fix backported into it (such as RedHat provides). Note that Debian's 5.3.3 version is NOT supported.

The runtime checks have been removed due to this version issue. To see if password_compat is available for your system, run the included version-test.php. If it outputs "Pass", you can safely use the library. If not, you cannot.

If you attempt to use password-compat on an unsupported version, attempts to create or verify hashes will return false. You have been warned!

The reason for this is that PHP prior to 5.3.7 contains a security issue with its BCRYPT implementation. Therefore, it's highly recommended that you upgrade to a newer version of PHP prior to using this layer.

Installation

To install, simply require the password.php file under lib.

You can also install it via Composer by using the Packagist archive.

Usage

Creating Password Hashes

To create a password hash from a password, simply use the password_hash function.

    $hash = password_hash($password, PASSWORD_BCRYPT);

Note that the algorithm that we chose is PASSWORD_BCRYPT. That's the current strongest algorithm supported. This is the BCRYPT crypt algorithm. It produces a 60 character hash as the result.

BCRYPT also allows for you to define a cost parameter in the options array. This allows for you to change the CPU cost of the algorithm:

    $hash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 10));

That's the same as the default. The cost can range from 4 to 31. I would suggest that you use the highest cost that you can, while keeping response time reasonable (I target between 0.1 and 0.5 seconds for a hash, depending on use-case).

Another algorithm name is supported:

    PASSWORD_DEFAULT

This will use the strongest algorithm available to PHP at the current time. Presently, this is the same as specifying PASSWORD_BCRYPT. But in future versions of PHP, it may be updated to use a stronger algorithm if one is introduced. It can also be changed if a problem is identified with the BCRYPT algorithm. Note that if you use this option, you are strongly encouraged to store it in a VARCHAR(255) column to avoid truncation issues if a future algorithm increases the length of the generated hash.

It is very important that you should check the return value of password_hash prior to storing it, because false or null may be returned if it encountered an error.

Verifying Password Hashes

To verify a hash created by password_hash, simply call:

	if (password_verify($password, $hash)) {
		/* Valid */
	} else {
		/* Invalid */
	}

That's all there is to it.

Rehashing Passwords

From time to time you may update your hashing parameters (algorithm, cost, etc). So a function to determine if rehashing is necessary is available:

    if (password_verify($password, $hash)) {
		if (password_needs_rehash($hash, $algorithm, $options)) {
			$hash = password_hash($password, $algorithm, $options);
			/* Store new hash in db */
		}
	}

Security Vulnerabilities

If you have found a security issue, please contact the author directly at [email protected].

Comments
  • Raw length of salt should be 16

    Raw length of salt should be 16

    I think it is bad to require 17 bytes of entropy for the salt, when only 16 bytes are needed. The underlying bcrypt function in crypt expects only 128 bits in the salt.

    opened by TerjeBr 18
  • [ISSUE] Integers aren't

    [ISSUE] Integers aren't "strings" so exception is thrown.

    I generated a 6 digit passcode for a users password using mt_rand this returned an int, when it comes to hashing the int it errors.

    https://github.com/ircmaxell/password_compat/blob/master/lib/password.php#L31

    opened by bweston92 10
  • Refactored the methods into a HashPassword class

    Refactored the methods into a HashPassword class

    Refactored the methods into a HashPassword class for better code organization when using OOP in projects.

    $pwObj = new HashPassword(); $hashedPasswd = $pwObj->password_hash($passwd, PASSWORD_BCRYPT, ["cost" => 10]); echo $hashedPasswd;

    opened by maxxon15 10
  • compatibility with sha512

    compatibility with sha512

    These changes add support for sha512 hashing algorithm. This is often required for compatibility with applications that don't support bcrypt.

    I am far from an expert on this level, but these changes work fine on our setup. What are your thoughts?

    opened by fredericve 9
  • No hash being returned with PHP5.3.8

    No hash being returned with PHP5.3.8

    Hello, I just implemented this excellent library (thanks for creating it) a few days ago.

    Now I got a report from a developer using PHP 5.3.8-ZS5.5.0 (cli) - which is perfectly matching the requirements - that password_hash returns false instead of a hash.

    Seems like crypt() function reported a failure, but I have no clue how to find out why. Testing the same against PHP 5.3.14 works without any problems.

    Any idea ?

    opened by bretrzaun 9
  • Function not outputting the same hash as built-in function

    Function not outputting the same hash as built-in function

    The function doesn't output the same hash as PHP 7.2.

    PHP 5.3.10 using this function $hash = password_hash('mypassword', PASSWORD_BCRYPT, array('cost' => 10)); $hash is $2y$10$mdrfl9XUF9J/qe2wnxopNevC1HtAcVxmYz9JSoetyABJggR7aMmNe

    PHP 7.2.24 calling native password_hash() $hash = password_hash('mypassword', PASSWORD_BCRYPT, array('cost' => 10)); $hash is $2y$10$HV3YTr/Mg2KvijZzUz9VDet.dlLbeKqToYHhodFCnCURgZVz.VEy2

    Am I missing something?

    opened by jjmontgo 8
  • This Repo

    This Repo

    Hi,

    I'm just wondering whether this repo is still being actively maintained by its owner? I'd been recently considering trying to implement something like what this package is intended to do, and it seems like package could be quite useful, but I can see four open pull requests to this repo currently, the oldest open since 2013 and the newest since 2015, and a number of forks with commits made since after the last commit to the parent; Not sure whether it would be better to work directly from the parent, from one of the forks, or something else.

    Your input appreciated, and cheers. :-)

    opened by Maikuolan 7
  • Question: What should happen when an incompatible hash is passed to password_verify?

    Question: What should happen when an incompatible hash is passed to password_verify?

    The documentation says that the hash of password_hash should be passed as hash parameter to password_verify. It does not say anything about the result when this value contains something else.

    I store the result of password_hash in a database and check it against a password later on (as documented). But if the password is not initialized, an empty value is passed to password_verify. The verification fails as crypt() makes up its own salt, but that was for this particular case.

    Are there any guarantees on the validation result when the hash is not exactly a return value of password_hash? (note that the password argument can be anything as this is passed by the user)

    opened by Lekensteyn 7
  • Next tagged release?

    Next tagged release?

    Hello,

    I'd like to know when the next tagged release will be? Your last tag is from over a year ago and there are some fixes that I'd like in production.

    Thanks!

    opened by stevetauber 6
  • Removed use of type-juggling comparison operators

    Removed use of type-juggling comparison operators

    Removed use of type-juggling comparison operators. The type-comparing comparision operators are safer to use in a security-sensitive code and faster [1].

    [1] http://stackoverflow.com/questions/6356826/comparing-versus

    opened by matjon 6
  • password_hash is returning wrong value on error

    password_hash is returning wrong value on error

    altough it states that returns false on error, most of the error checks return null instead, so a " === false " comparison would fail. also, it is inconsistent with password_verify that returns false on "crypt missing" error

    opened by einacio 6
  • PHPCompatibility ruleset for password_compat

    PHPCompatibility ruleset for password_compat

    Hi all,

    This is just a "service message".

    For those people who use this library and use PHPCompatibility in their CI process, there is now a custom ruleset available which can be used to prevent false positives being thrown by PHPCompatibility for the native PHP functionality being polyfilled by this repo.

    You can find the repo for the PHPCompatibilityPasswordCompat ruleset here on Github as well as on Packagist.

    • https://github.com/PHPCompatibility/PHPCompatibilityPasswordCompat
    • https://packagist.org/packages/phpcompatibility/phpcompatibility-passwordcompat

    Hope someone will find it useful :smile:

    P.S.: If anyone is interested in helping us to maintain the ruleset, please open an issue in the repo.

    opened by jrfnl 0
  • php7 Compatibility problem

    php7 Compatibility problem

    105 | ERROR | Function mcrypt_create_iv() is deprecated since PHP 7.1 and removed since PHP 7.2; Use random_bytes() or OpenSSL instead 105 | ERROR | Extension 'mcrypt' is deprecated since PHP 7.1 and removed since PHP 7.2; Use openssl (preferred) or pecl/mcrypt once available instead 105 | ERROR | The constant "MCRYPT_DEV_URANDOM" is deprecated since PHP 7.1 and removed since PHP 7.2

    opened by oasfuyou 5
  • password_verify fails for hashes from crypt()

    password_verify fails for hashes from crypt()

    When I run the following code in PHP 5.4.45

    $password = 'XXX';
    $salt = 'XX';
    var_dump(password_verify($password, crypt($password, $salt)));
    

    I get false as result. When I run the same code with PHP's native password_verify function, I get true

    opened by flack 3
  • Suggestion: use *.phpt tests from php-src to improve compatibility

    Suggestion: use *.phpt tests from php-src to improve compatibility

    This should be pretty easy to do, since PHPUnit does support *.phpt format. All that is needed is to copy them from php-src and add

    --INI--
    auto_prepend_file=lib/password.php
    

    to each test file.

    opened by weirdan 0
Owner
Anthony Ferrara
Anthony Ferrara
2 functions which work together to sanitize the the information from a form from SQL_Inyection.

Form_sanitizer 2 functions which work together to sanitize the the information from a form from SQL_Inyection. How to use the 2 functions Once you cop

Gorrian 1 Jul 19, 2022
PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web application

PHPIDS PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web ap

null 752 Jan 3, 2023
php-chmod is a PHP library for easily changing permissions recursively.

PHP chmod php-chmod is a PHP library for easily changing the permissions recursively. Versions & Dependencies Version PHP Documentation ^1.1 ^7.4 curr

Mathias Reker ⚡️ 5 Oct 7, 2022
PHP 5.x support for random_bytes() and random_int()

random_compat PHP 5.x polyfill for random_bytes() and random_int() created and maintained by Paragon Initiative Enterprises. Although this library sho

Paragon Initiative Enterprises 8k Jan 5, 2023
PHP Secure Communications Library

phpseclib - PHP Secure Communications Library Supporting phpseclib Become a backer or sponsor on Patreon One-time donation via PayPal or crypto-curren

null 4.9k Jan 7, 2023
Simple Encryption in PHP.

php-encryption composer require defuse/php-encryption This is a library for encrypting data with a key or password in PHP. It requires PHP 5.6 or new

Taylor Hornby 3.6k Jan 3, 2023
Standards compliant HTML filter written in PHP

HTML Purifier HTML Purifier is an HTML filtering solution that uses a unique combination of robust whitelists and aggressive parsing to ensure that no

Edward Z. Yang 2.7k Jan 5, 2023
A database of PHP security advisories

PHP Security Advisories Database The PHP Security Advisories Database references known security vulnerabilities in various PHP projects and libraries.

null 1.9k Dec 18, 2022
A php.ini scanner for best security practices

Scanner for PHP.ini The Iniscan is a tool designed to scan the given php.ini file for common security practices and report back results. Currently it

psec.io 1.5k Dec 5, 2022
🤖 Id obfuscation based on Knuth's multiplicative hashing method for PHP.

Optimus id transformation With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar

Jens Segers 1.2k Jan 2, 2023
㊙️ AntiXSS | Protection against Cross-site scripting (XSS) via PHP

㊙️ AntiXSS "Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications. XSS enables attackers to inje

Lars Moelleken 570 Dec 16, 2022
An experimental object oriented SSH api in PHP

PHP SSH (master) Provides an object-oriented wrapper for the php ssh2 extension. Requirements You need PHP version 5.3+ with the SSH2 extension. Insta

Antoine Hérault 355 Dec 6, 2022
TCrypto is a simple and flexible PHP 5.3+ in-memory key-value storage library

About TCrypto is a simple and flexible PHP 5.3+ in-memory key-value storage library. By default, a cookie will be used as a storage backend. TCrypto h

timoh 57 Dec 2, 2022
Fetches random integers from random.org instead of using PHP's PRNG implementation

TrulyRandom Composer-compatible library to interact with random.org's API in order to generate truly random lists of integers, sequences of integers,

Erik Wurzer 46 Nov 25, 2022
PHPGGC is a library of PHP unserialize() payloads along with a tool to generate them, from command line or programmatically.

PHPGGC: PHP Generic Gadget Chains PHPGGC is a library of unserialize() payloads along with a tool to generate them, from command line or programmatica

Ambionics Security 2.5k Jan 4, 2023
Let's Encrypt/ACME Command Line client written in PHP

Acme PHP Acme PHP is a simple yet very extensible CLI client for Let's Encrypt that will help you get and renew free HTTPS certificates. Acme PHP is a

Acme PHP 539 Dec 30, 2022
PHP Malware Finder

PHP Malware Finder _______ __ __ _______ | ___ || |_| || | | | | || || ___| | |___| || || |___ Webshell finder, |

NBS System 205 Dec 24, 2022
A proof of concept of a PHP Miner that can mine DuinoCoin

Duino Coin - PHP Miner This is a proof of concept. This miner is provided as is, with no guarantee it will work as intended for you.

Ricardo Fiorani 10 Sep 7, 2022
Port scanning using PHP!

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ?? Scanner Port's ?? ???? Don't forget to leave a star! ⭐ ???? Não se esqueça de deixar uma estrela! ⭐ ?? Credits | Créd

Hellen. 4 Feb 26, 2022