add type annotation

This commit is contained in:
Pig Fang 2020-06-02 15:46:04 +08:00
parent d40bc66438
commit 069ec27e6f
11 changed files with 24 additions and 63 deletions

View File

@ -4,12 +4,12 @@ namespace App\Services\Cipher;
class ARGON2I extends BaseCipher class ARGON2I extends BaseCipher
{ {
public function hash($value, $salt = '') public function hash($value, $salt = ''): string
{ {
return password_hash($value, PASSWORD_ARGON2I); return password_hash($value, PASSWORD_ARGON2I);
} }
public function verify($password, $hash, $salt = '') public function verify($password, $hash, $salt = ''): bool
{ {
return password_verify($password, $hash); return password_verify($password, $hash);
} }

View File

@ -4,12 +4,12 @@ namespace App\Services\Cipher;
class BCRYPT extends BaseCipher class BCRYPT extends BaseCipher
{ {
public function hash($value, $salt = '') public function hash($value, $salt = ''): string
{ {
return password_hash($value, PASSWORD_BCRYPT); return password_hash($value, PASSWORD_BCRYPT);
} }
public function verify($password, $hash, $salt = '') public function verify($password, $hash, $salt = ''): bool
{ {
return password_verify($password, $hash); return password_verify($password, $hash);
} }

View File

@ -2,19 +2,24 @@
namespace App\Services\Cipher; namespace App\Services\Cipher;
abstract class BaseCipher implements EncryptInterface abstract class BaseCipher
{ {
/** /**
* {@inheritdoc} * Encrypt given string with given salt.
*
* @param string $value
* @param string $salt
*/ */
public function hash($value, $salt = '') public abstract function hash($value, $salt = '');
{
}
/** /**
* {@inheritdoc} * Verify that the given hash matches the given password.
*
* @param string $password
* @param string $hash
* @param string $salt
*/ */
public function verify($password, $hash, $salt = '') public function verify($password, $hash, $salt = '')
{ {
return hash_equals($hash, $this->hash($password, $salt)); return hash_equals($hash, $this->hash($password, $salt));
} }

View File

@ -1,26 +0,0 @@
<?php
namespace App\Services\Cipher;
interface EncryptInterface
{
/**
* Encrypt given string with given salt.
*
* @param string $value
* @param string $salt
*
* @return string
*/
public function hash($value, $salt = '');
/**
* Verifies that the given hash matches the given password.
*
* @param string $password
* @param string $hash
*
* @return bool
*/
public function verify($password, $hash);
}

View File

@ -4,10 +4,7 @@ namespace App\Services\Cipher;
class MD5 extends BaseCipher class MD5 extends BaseCipher
{ {
/** public function hash($value, $salt = ''): string
* Once MD5 hash.
*/
public function hash($value, $salt = '')
{ {
return md5($value); return md5($value);
} }

View File

@ -4,12 +4,12 @@ namespace App\Services\Cipher;
class PHP_PASSWORD_HASH extends BaseCipher class PHP_PASSWORD_HASH extends BaseCipher
{ {
public function hash($value, $salt = '') public function hash($value, $salt = ''): string
{ {
return password_hash($value, PASSWORD_DEFAULT); return password_hash($value, PASSWORD_DEFAULT);
} }
public function verify($password, $hash, $salt = '') public function verify($password, $hash, $salt = ''): bool
{ {
return password_verify($password, $hash); return password_verify($password, $hash);
} }

View File

@ -4,10 +4,7 @@ namespace App\Services\Cipher;
class SALTED2MD5 extends BaseCipher class SALTED2MD5 extends BaseCipher
{ {
/** public function hash($value, $salt = ''): string
* MD5 hash with salt.
*/
public function hash($value, $salt = '')
{ {
return md5(md5($value).$salt); return md5(md5($value).$salt);
} }

View File

@ -4,10 +4,7 @@ namespace App\Services\Cipher;
class SALTED2SHA256 extends BaseCipher class SALTED2SHA256 extends BaseCipher
{ {
/** public function hash($value, $salt = ''): string
* SHA256 hash with salt.
*/
public function hash($value, $salt = '')
{ {
return hash('sha256', hash('sha256', $value).$salt); return hash('sha256', hash('sha256', $value).$salt);
} }

View File

@ -4,10 +4,7 @@ namespace App\Services\Cipher;
class SALTED2SHA512 extends BaseCipher class SALTED2SHA512 extends BaseCipher
{ {
/** public function hash($value, $salt = ''): string
* SHA512 hash with salt.
*/
public function hash($value, $salt = '')
{ {
return hash('sha512', hash('sha512', $value).$salt); return hash('sha512', hash('sha512', $value).$salt);
} }

View File

@ -4,10 +4,7 @@ namespace App\Services\Cipher;
class SHA256 extends BaseCipher class SHA256 extends BaseCipher
{ {
/** public function hash($value, $salt = ''): string
* Once SHA256 hash.
*/
public function hash($value, $salt = '')
{ {
return hash('sha256', $value); return hash('sha256', $value);
} }

View File

@ -4,10 +4,7 @@ namespace App\Services\Cipher;
class SHA512 extends BaseCipher class SHA512 extends BaseCipher
{ {
/** public function hash($value, $salt = ''): string
* Once SHA512 hash.
*/
public function hash($value, $salt = '')
{ {
return hash('sha512', $value); return hash('sha512', $value);
} }