Skip to content

Commit

Permalink
#11253 Check hash algorithm availability
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed Oct 6, 2017
1 parent b3dd206 commit 9fe8944
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Utility/Security.php
Expand Up @@ -66,6 +66,15 @@ public static function hash($string, $type = null, $salt = false)
}
$type = strtolower($type);

$hashAlgos = hash_algos();
if (!in_array($type, $hashAlgos)) {
throw new RuntimeException(sprintf(
'The hash type `%s` was not found. Available algorithms are: %s',
$type,
implode(', ', $hashAlgos)
));
}

if ($salt) {
if (!is_string($salt)) {
$salt = static::$_salt;
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Utility/SecurityTest.php
Expand Up @@ -69,6 +69,20 @@ public function testHash()
Security::setHash($_hashType);
}

/**
* testInvalidHashTypeException
*
* @return void
*/
public function testInvalidHashTypeException() {
try {
Security::hash('test', 'doesnotexist', false);
$this->fail('Expected \RuntimeException');
} catch (\RuntimeException $e) {
$this->assertTextContains('The hash type `doesnotexist` was not found. Available algorithms are:', $e->getMessage());
}
}

/**
* testRijndael method
*
Expand Down

0 comments on commit 9fe8944

Please sign in to comment.