Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple hashing algorithm option #68

Merged
merged 1 commit into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Authentication/LocalAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function validate(array $credentials, bool $returnUser=false)
// This would be due to the hash algorithm or hash
// cost changing since the last time that a user
// logged in.
if (password_needs_rehash($user->password_hash, PASSWORD_DEFAULT))
if (password_needs_rehash($user->password_hash, $this->config->hashAlgorithm))
{
$user->password = $password;
$this->userModel->save($user);
Expand Down
17 changes: 17 additions & 0 deletions src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ class Auth extends BaseConfig
//
public $silent = false;

// Valid values are PASSWORD_DEFAULT, PASSWORD_BCRYPT and PASSWORD_ARGON2I.
public $hashAlgorithm = PASSWORD_ARGON2I;

//--------------------------------------------------------------------
// ARGON2i Algorithm options
//--------------------------------------------------------------------
// The ARGON2I method of encryption allows you to define the "memory_cost",
// the "time_cost" and the number of "threads", whenever a password hash is created.
// This defaults to a value of 10 which is an acceptable number.
// However, depending on the security needs of your application
// and the power of your hardware, you might want to increase the
// cost. This makes the hashing process takes longer.
//
public $hashMemoryCost = PASSWORD_ARGON2_DEFAULT_MEMORY_COST; // 1024
public $hashTimeCost = PASSWORD_ARGON2_DEFAULT_TIME_COST; // 2
public $hashThreads = PASSWORD_ARGON2_DEFAULT_THREADS; // 2

//--------------------------------------------------------------------
// Password Hashing Cost
//--------------------------------------------------------------------
Expand Down
29 changes: 22 additions & 7 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,28 @@ public function setPassword(string $password)
{
$config = config('Auth');

$this->attributes['password_hash'] = password_hash(
base64_encode(
hash('sha384', $password, true)
),
PASSWORD_DEFAULT,
['cost' => $config->hashCost]
);
if($config->hashAlgorithm == PASSWORD_ARGON2I)
{
$hashOptions = [
'memory_cost' => $config->hashMemoryCost,
'time_cost' => $config->hashTimeCost,
'threads' => $config->hashThreads
];
}
else
{
$hashOptions = [
'cost' => $config->hashCost
];
}

$this->attributes['password_hash'] = password_hash(
base64_encode(
hash('sha384', $password, true)
),
$config->hashAlgorithm,
$hashOptions
);
}

/**
Expand Down