<?php require __DIR__ . '/vendor/autoload.php'; use mindplay\blowfish\BlowfishService; test('Entropy functions work as expected', function () { $service = new BlowfishService(); $entropy = invoke($service, 'getEntropy', array(10)); ok(strlen($entropy) === 10, 'returns 10 bytes of entropy'); $entropy = invoke($service, 'getEntropy', array(100)); ok(strlen($entropy) === 100, 'returns 100 bytes of entropy'); }); test('Can hash and check passwords', function () { foreach (array(4, 10) as $cost) { $service = new BlowfishService($cost); foreach (array('x', 'p@s$w0Rd', 'KytmCwqjb6wYPGgEHZ55DRfDanNVWwxnmMMnzCRu72ghQ89S') as $password) { $hash = $service->hash($password); ok($service->check($password, $hash), 'password verified (with cost ' . $cost . ')', $password); ok($service->check($password . '-', $hash) === false, 'invalid password rejected (with cost ' . $cost . ')', $password); ok($service->check($password, $hash . '-') === false, 'invalid hash rejected (with cost ' . $cost . ')', $password); } } }); exit(status()); // https://gist.github.com/mindplay-dk/4260582 /** * @param string $name test description * @param callable $function test implementation */ function test($name, $function) { echo "\n=== {$name} ===\n\n";
<?php namespace mindplay\blowfish; use RuntimeException; BlowfishService::init(); /** * This class provides a simple wrapper around the Blowfish cipher. * * This class will throw (immediately on load) on PHP versions prior to 5.3.7, which * had a broken implementation of the Blowfish algorithm (and/or would fall back to DES.) * * http://www.php.net/security/crypt_blowfish.php */ class BlowfishService { /** @type int salt length required for Blowfish algorithm */ const SALT_LENGTH = 16; /** @type string path to the dev/urandom device on Linux */ const DEV_URANDOM = '/dev/urandom'; /** @type string minimum PHP version with proper Blowfish support */ const MIN_PHP_VERSION = '5.3.7'; /** * @var int cryptographic cost of the Blowfish algorithm */ private $_cost; /** * @param int $cost cost (iteration count) for the underlying Blowfish-based hashing algorithm (range 4 to 31) * * @throws RuntimeException for invalid $cost */