2022-05-02 02:13:59 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Auth;
|
|
|
|
|
|
|
|
abstract class Hash
|
|
|
|
{
|
|
|
|
/**
|
2022-06-22 01:59:52 +12:00
|
|
|
* @var array $options Hashing-algo specific options
|
2022-05-02 02:13:59 +12:00
|
|
|
*/
|
2022-06-22 01:59:52 +12:00
|
|
|
protected array $options = [];
|
2022-06-14 23:08:54 +12:00
|
|
|
|
2022-05-02 02:13:59 +12:00
|
|
|
/**
|
2022-06-22 01:59:52 +12:00
|
|
|
* @param array $options Hashing-algo specific options
|
2022-05-02 02:13:59 +12:00
|
|
|
*/
|
2022-06-22 01:59:52 +12:00
|
|
|
public function __construct(array $options = [])
|
2022-06-14 23:08:54 +12:00
|
|
|
{
|
2022-05-02 02:13:59 +12:00
|
|
|
$this->setOptions($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set hashing algo options
|
2022-06-14 23:08:54 +12:00
|
|
|
*
|
2022-06-22 01:59:52 +12:00
|
|
|
* @param array $options Hashing-algo specific options
|
2022-05-02 02:13:59 +12:00
|
|
|
*/
|
2022-06-22 01:59:52 +12:00
|
|
|
public function setOptions(array $options): self
|
2022-06-14 23:08:54 +12:00
|
|
|
{
|
2022-05-02 02:13:59 +12:00
|
|
|
$this->options = \array_merge([], $this->getDefaultOptions(), $options);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get hashing algo options
|
2022-06-14 23:08:54 +12:00
|
|
|
*
|
2022-06-22 01:59:52 +12:00
|
|
|
* @return array $options Hashing-algo specific options
|
2022-05-02 02:13:59 +12:00
|
|
|
*/
|
2022-06-22 01:59:52 +12:00
|
|
|
public function getOptions(): array
|
2022-06-14 23:08:54 +12:00
|
|
|
{
|
2022-05-02 02:13:59 +12:00
|
|
|
return $this->options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $password Input password to hash
|
2022-06-14 23:08:54 +12:00
|
|
|
*
|
2022-05-02 02:13:59 +12:00
|
|
|
* @return string hash
|
|
|
|
*/
|
|
|
|
abstract public function hash(string $password): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $password Input password to validate
|
|
|
|
* @param string $hash Hash to verify password against
|
2022-06-14 23:08:54 +12:00
|
|
|
*
|
2022-05-02 02:13:59 +12:00
|
|
|
* @return boolean true if password matches hash
|
|
|
|
*/
|
|
|
|
abstract public function verify(string $password, string $hash): bool;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default options for specific hashing algo
|
2022-06-14 23:08:54 +12:00
|
|
|
*
|
2022-06-22 01:59:52 +12:00
|
|
|
* @return array options named array
|
2022-05-02 02:13:59 +12:00
|
|
|
*/
|
2022-06-22 01:59:52 +12:00
|
|
|
abstract public function getDefaultOptions(): array;
|
2022-05-02 02:13:59 +12:00
|
|
|
}
|