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

52 lines
1.2 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Auth\Hash;
use Appwrite\Auth\Hash;
/*
2022-06-17 09:25:28 +00:00
* Scrypt accepted options:
* string? salt; auto-generated if empty
2022-06-16 09:21:35 +00:00
* int costCpu
* int costMemory
* int costParallel
* 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();
2022-06-16 09:21:35 +00:00
return \scrypt($password, $options['salt'] ?? null, $options['costCpu'], $options['costMemory'], $options['costParallel'], $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
{
2022-06-16 09:21:35 +00:00
return [ 'costCpu' => 8, 'costMemory' => 14, 'costParallel' => 1, 'length' => 64 ];
}
2022-06-14 11:08:54 +00:00
}