silverstripe-framework/src/Security/PasswordEncryptor_PHPHash.php

45 lines
993 B
PHP
Raw Normal View History

<?php
namespace SilverStripe\Security;
use Exception;
/**
* Encryption using built-in hash types in PHP.
* Please note that the implemented algorithms depend on the PHP
* distribution and architecture.
*/
class PasswordEncryptor_PHPHash extends PasswordEncryptor
{
2016-11-29 00:31:16 +01:00
protected $algorithm = 'sha1';
/**
* @param string $algorithm A PHP built-in hashing algorithm as defined by hash_algos()
* @throws Exception
*/
public function __construct($algorithm)
{
if (!in_array($algorithm, hash_algos())) {
throw new Exception(
sprintf('Hash algorithm "%s" not found in hash_algos()', $algorithm)
);
}
$this->algorithm = $algorithm;
}
/**
* @return string
*/
public function getAlgorithm()
{
return $this->algorithm;
}
public function encrypt($password, $salt = null, $member = null)
{
return hash($this->algorithm, $password . $salt);
}
}