2022-05-01 14:13:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Appwrite\Auth\Hash;
|
|
|
|
|
|
|
|
|
|
use Appwrite\Auth\Hash;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* SCrypt accepted options:
|
|
|
|
|
* string? salt; auto-generated if empty
|
|
|
|
|
* int cost_cpu
|
|
|
|
|
* int cost_memory
|
|
|
|
|
* int cost_parallel
|
|
|
|
|
* int length
|
2022-06-14 11:08:54 +00:00
|
|
|
*
|
2022-05-01 14:13:59 +00:00
|
|
|
* Refference: https://github.com/DomBlack/php-scrypt/blob/master/scrypt.php#L112-L116
|
|
|
|
|
*/
|
2022-05-15 06:53:26 +00:00
|
|
|
class Scrypt extends Hash
|
2022-05-01 14:13:59 +00:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param string $password Input password to hash
|
2022-06-14 11:08:54 +00:00
|
|
|
*
|
2022-05-01 14:13:59 +00:00
|
|
|
* @return string hash
|
|
|
|
|
*/
|
2022-06-14 11:08:54 +00:00
|
|
|
public function hash(string $password): string
|
|
|
|
|
{
|
2022-05-01 14:13:59 +00:00
|
|
|
$options = $this->getOptions();
|
|
|
|
|
|
|
|
|
|
return \scrypt($password, $options['salt'] ?? null, $options['cost_cpu'], $options['cost_memory'], $options['cost_parallel'], $options['length']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $password Input password to validate
|
|
|
|
|
* @param string $hash Hash to verify password against
|
2022-06-14 11:08:54 +00:00
|
|
|
*
|
2022-05-01 14:13:59 +00:00
|
|
|
* @return boolean true if password matches hash
|
|
|
|
|
*/
|
2022-06-14 11:08:54 +00:00
|
|
|
public function verify(string $password, string $hash): bool
|
|
|
|
|
{
|
2022-06-14 09:05:46 +00:00
|
|
|
return $hash === $this->hash($password);
|
2022-05-01 14:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get default options for specific hashing algo
|
2022-06-14 11:08:54 +00:00
|
|
|
*
|
2022-05-01 14:13:59 +00:00
|
|
|
* @return mixed options named array
|
|
|
|
|
*/
|
2022-06-14 11:08:54 +00:00
|
|
|
public function getDefaultOptions(): mixed
|
|
|
|
|
{
|
2022-05-01 14:13:59 +00:00
|
|
|
return [ 'cost_cpu' => 8, 'cost_memory' => 14, 'cost_parallel' => 1, 'length' => 64 ];
|
|
|
|
|
}
|
2022-06-14 11:08:54 +00:00
|
|
|
}
|