appwrite/src/Appwrite/Auth/Hash/Scrypt.php

52 lines
1.3 KiB
PHP
Raw Normal View History

<?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
*
* 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
{
/**
* @param string $password Input password to hash
2022-06-14 11:08:54 +00:00
*
* @return string hash
*/
2022-06-14 11:08:54 +00:00
public function hash(string $password): string
{
$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
*
* @return boolean true if password matches hash
*/
2022-06-14 11:08:54 +00:00
public function verify(string $password, string $hash): bool
{
return $hash === $this->hash($password);
}
/**
* Get default options for specific hashing algo
2022-06-14 11:08:54 +00:00
*
* @return mixed options named array
*/
2022-06-14 11:08:54 +00:00
public function getDefaultOptions(): mixed
{
return [ 'cost_cpu' => 8, 'cost_memory' => 14, 'cost_parallel' => 1, 'length' => 64 ];
}
2022-06-14 11:08:54 +00:00
}