forked from AuthMe/AuthMeReloaded
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AuthMe#685 Add php implementation for PBKDF2
- Create php sample for PBKDF2 - Rename pbkdf2 java classes (remove Crypt prefix) - Remove options from hash setting comment that should not be used
- Loading branch information
Showing
9 changed files
with
75 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/*********************************************************** | ||
* AuthMe website integration logic for PBKDF2 * | ||
* ------------------------------------------------------- * | ||
* See AuthMeController for details. * | ||
* * | ||
* Source: https://github.com/AuthMe-Team/AuthMeReloaded/ * | ||
***********************************************************/ | ||
class Pbkdf2 extends AuthMeController { | ||
|
||
/** @var string[] range of characters for salt generation */ | ||
private $CHARS; | ||
|
||
const SALT_LENGTH = 16; | ||
const NUMBER_OF_ITERATIONS = 10000; | ||
|
||
public function __construct() { | ||
$this->CHARS = self::initCharRange(); | ||
} | ||
|
||
protected function isValidPassword($password, $hash) { | ||
// hash := pbkdf2_sha256$iterations$salt$hash | ||
$parts = explode('$', $hash); | ||
return count($parts) === 4 && $hash === $this->computeHash($parts[1], $parts[2], $password); | ||
} | ||
|
||
protected function hash($password) { | ||
$salt = $this->generateSalt(); | ||
return $this->computeHash(self::NUMBER_OF_ITERATIONS, $salt, $password); | ||
} | ||
|
||
private function computeHash($iterations, $salt, $password) { | ||
return 'pbkdf2_sha256$' . self::NUMBER_OF_ITERATIONS . '$' . $salt | ||
. '$' . hash_pbkdf2('sha256', $password, $salt, self::NUMBER_OF_ITERATIONS, 64, false); | ||
} | ||
|
||
/** | ||
* @return string randomly generated salt | ||
*/ | ||
private function generateSalt() { | ||
$maxCharIndex = count($this->CHARS) - 1; | ||
$salt = ''; | ||
for ($i = 0; $i < self::SALT_LENGTH; ++$i) { | ||
$salt .= $this->CHARS[mt_rand(0, $maxCharIndex)]; | ||
} | ||
return $salt; | ||
} | ||
|
||
private static function initCharRange() { | ||
return array_merge(range('0', '9'), range('a', 'f')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...ecurity/crypts/CryptPBKDF2DjangoTest.java → ...hme/security/crypts/Pbkdf2DjangoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...thme/security/crypts/CryptPBKDF2Test.java → ...hi/authme/security/crypts/Pbkdf2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters