2015-11-21 14:27:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gist\Service;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
|
|
|
/**
|
2016-11-13 00:44:23 +01:00
|
|
|
* Class SaltGenerator.
|
|
|
|
*
|
2015-11-21 14:27:53 +01:00
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class SaltGenerator
|
|
|
|
{
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* Generates a random salt.
|
|
|
|
*
|
|
|
|
* @param int $length
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-11-23 11:53:24 +01:00
|
|
|
public function generate($length = 32)
|
2015-11-21 14:27:53 +01:00
|
|
|
{
|
|
|
|
if (!is_numeric($length)) {
|
|
|
|
throw new InvalidArgumentException('Paramter length must be a valid integer.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function_exists('openssl_random_pseudo_bytes')) {
|
|
|
|
return substr(base64_encode(openssl_random_pseudo_bytes($length)), 0, $length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function_exists('mcrypt_create_iv')) {
|
|
|
|
return substr(base64_encode(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)), 0, $length);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException('You must enable openssl or mcrypt modules.');
|
|
|
|
}
|
|
|
|
}
|