Giter Site home page Giter Site logo

phpass's Introduction

PHP Password Library

The PHP Password Library is designed to ease the tasks associated with working with passwords in PHP. It is capable of generating strong cryptographic password hashes, verifying supplied password strings against those hashes, and calculating the strength of a password string using various algorithms.

This project was inspired by Openwall's portable hashing library for PHP and PassLib for Python.

Features

  • Create and verify secure password hashes with only a few lines of code.
  • Supports bcrypt and PBKDF2 out of the box.
  • Easily extend to support additional hashing methods.
  • Additional password strength component based on well-known algorithms.
  • Follows the PSR-0 standard for autoloader compatibility.

Installation

PEAR

Installing via PEAR is a simple matter of including the PEAR channel and installing the rych/PHPass package.

pear channel-discover rchouinard.github.com/pear
pear install rych/PHPass-2.1.0-alpha

Composer

Composer is an easy way to manage dependencies in your PHP projects. The PHP Password Library can be found in the default Packagist repository.

After installing Composer into your project, the PHP Password Library can be installed by adding the following lines to your composer.json file and running the Composer command line tool:

{
  "require": {
    "rych/phpass": "2.1.0-dev"
  }
}

Usage

Hashing passwords

The library provides the ability to generate strong cryptographic hashes of user passwords using a variety of methods. Each method may be customized as needed, and may also be combined with HMAC hashing when using the base class.

Examples

Use the default bcrypt adapter:

<?php
// Default configuration - bcrypt adapter, 2^12 (4,096) iterations
$phpassHash = new \Phpass\Hash;

Use the PBKDF2 adapter:

<?php
// Customize hash adapter - PBKDF2 adapter, 15,000 iterations
$adapter = new \Phpass\Hash\Adapter\Pbkdf2(array (
    'iterationCount' => 15000
));
$phpassHash = new \Phpass\Hash($adapter);

Create and verify a password hash:

<?php
// Create and verify a password hash from any of the above configurations
$passwordHash = $phpassHash->hashPassword($password);
if ($phpassHash->checkPassword($password, $passwordHash)) {
    // Password matches...
} else {
    // Password doesn't match...
}

Calculating password strength

There are many different ways to calculate the relative strength of a given password, and this library supports a few of the most common. Each method returns a number which represents the estimated entropy for the given password. It's up to the developer to determine the minimum calculated entropy to accept. Combined with a sensible password policy, this can be a valuable tool in selecting strong passwords.

Examples

Calculate a password's entropy using NIST recommendations:

<?php
// Default configuration (NIST recommendations)
$phpassStrength = new \Phpass\Strength;

// Returns 30
$passwordEntropy = $phpassStrength->calculate('MySecretPassword');

Calculate a password's entropy using Wolfram Alpha's algorithm:

<?php
// Custom strength adapter (Wolfram algorithm)
$adapter = new \Phpass\Strength\Adapter\Wolfram;
$phpassStrength = new \Phpass\Strength($adapter);

// Returns 59
$passwordEntropy = $phpassStrength->calculate('MySecretPassword');

phpass's People

Contributors

rchouinard avatar travispaul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpass's Issues

support for configurable $id in Phpass\Adapter\Portable.php

Me again.

I was wondering if it would be possible to change Portable.php to accept the id string ($H$) as config option.
This would be much cleaner than the current if ($id != '$P$' && $id != '$H$') stuff and also allow to make things like isValid() work too.

Cheers, mano

NIST-based entropy calculation should not be the default

The wikipedia article mentions the following:

Later research into human-selected password entropy using newly available real world data has demonstrated that the NIST scheme does not provide a valid metric for entropy estimation of human-selected passwords.

The linked blog post and research paper clarify this statement. NIST relies on the Shannon entropy model, which has been proven to incorrectly model real-world password entropy considering use by humans and real-life attack methods. Having this method as the default is therefore unwise and should be deprecated.

I'm not sure how the Wolfram Alpha calculation is constructed, so cannot say if it performs better, but from my personal tests it also has some weak spots. A method like Dropbox's zxcvbn algorithm seems to be a better match, although it hasn't been around long enough to be properly reviewed. Still it seems like a reasonable method that would be good to implement since it can be used in combination with the front-end javascript library. Something to consider though.

Last character in BCrypt Salt can only consist of one of four characters.

The BCrypt adapter takes 16 bytes of random data and tries to create a salt using 64 possible characters (a period, a slash, 26 uppercase letters, 26 lowercase letters, and 10 numerics).

It does this by taking six bits of data from the 16 bytes provided to create a number between 0-63 to index into an array of salt characters. When there is no more data in a given byte, it takes the next 2 or 4 bits from the next byte, or begins a new byte entirely.

The problem is that only 16 bytes of data are being used to create a 22 character salt. 16 bytes only provides 128 bits of data. 22 six-bit indexes would take 132 bits to create.

The way PHPass creates the final salt character is to take the remaining two bits from the final byte, prep them to receive the next four bits from the next byte, which doesn't exist, and writes this as the final character.

This means that the rightmost four bits in the final byte will always be zeroes, creating only four possible character indexes, 0, 16, 32, and 48 (i.e. 000000, 100000, 010000, and 110000). These indexes correspond to the period character, uppercase O, lowercase E, and lowercase U.

One of these four characters will always be the last character in the BCrypt salt.

You can fix this by providing 17 bytes of data to the _encode64 method, and then taking trimming the output from that method to only use the first 22 characters provided. If you have a better way, then by all means please use that.

namespace for Phpass.php

Hi there,

Just wondering if it would be possible to have Phpass.php in a namespace too? It seems weird to have all code nicely put into namespaces, but not the class you are bound to use?

One PHPUnit test fails in 2.0.2 (and 2.0.2-p1)

PHPUnit output for 2.0.2 and 2.0.2-p1

PHPUnit 3.6.11 by Sebastian Bergmann.

Configuration read from /home/jay/phpass/rchouinard-phpass-2dc6e32/tests/phpunit.xml

.....F.................................

Time: 8 seconds, Memory: 5.75Mb

There was 1 failure:

1) Phpass\Hash\Adapter\ExtDesTest::adapterGeneratesSameHashGivenOriginalSaltAndPasswordString
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'_zzz1OacgfuRYUi4zYeQ'
+'_zPXZFl8kpcQw'

/home/jay/phpass/rchouinard-phpass-2dc6e32/tests/library/Phpass/Hash/Adapter/ExtDesTest.php:133

FAILURES!
Tests: 39, Assertions: 57, Failures: 1.

Generating code coverage report in HTML format ... done

I've run the tests several times. The expected hash is different every time, but the actual stays the same.

Configuration (two machines):

$ php --version
PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:20:55)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

$ php --version
PHP 5.4.4-2 (cli) (built: Jun 19 2012 07:38:55)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.0, Copyright (c) 2002-2012, by Derick Rethans

PEAR 1.10.0 Using $this when not in object context in /usr/share/php/PEAR/Downloader.php

After updating to PEAR 1.10.0, channel-discover and channel-update for this channel give the following error:

pear channel-update rych
Updating channel "rych"
PHP Fatal error: Using $this when not in object context in /usr/share/php/PEAR/Downloader.php on line 1714
PHP Stack trace:
PHP 1. {main}() /usr/share/php/pearcmd.php:0
PHP 2. PEAR_Command_Common->run() /usr/share/php/pearcmd.php:316
PHP 3. PEAR_Command_Channels->doUpdate() /usr/share/php/PEAR/Command/Common.php:270
PHP 4. PEAR_Common->downloadHttp() /usr/share/php/PEAR/Command/Channels.php:546
PHP 5. PEAR_Downloader::_downloadHttp() /usr/share/php/PEAR/Common.php:833

works fine for all of my other channels

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.