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

Fixed Random::base64Safe #12150

Merged
merged 2 commits into from
Aug 17, 2016
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Fixed `Phalcon\Mvc\Model\Criteria::inWhere` so that now the second parameter can be an empty array [#10676](https://github.com/phalcon/cphalcon/issues/10676)
- Fixed ORM related memory leak [#12115](https://github.com/phalcon/cphalcon/issues/12115), [#11995](https://github.com/phalcon/cphalcon/issues/11995), [#12116](https://github.com/phalcon/cphalcon/issues/12116)
- Fixed incorrect `Phalcon\Mvc\View::getActiveRenderPath` behavior [#12139](https://github.com/phalcon/cphalcon/issues/12139)
- Fixed `Phalcon\Security\Random::base64Safe` so that now the method returns correct safe string [#12141](https://github.com/phalcon/cphalcon/issues/12141)

# [3.0.0](https://github.com/phalcon/cphalcon/releases/tag/v3.0.0) (2016-07-29)
- PHP 5.3 and 5.4 are now fully deprecated
Expand Down
5 changes: 3 additions & 2 deletions phalcon/security/random.zep
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,11 @@ class Random
{
var s;

let s = preg_replace("#[^a-z0-9_=-]+#i", "", this->base64(len));
let s = strtr(base64_encode(this->base64(len)), "+/", "-_");
let s = preg_replace("#[^a-z0-9_=-]+#i", "", s);

if !padding {
return trim(s, "=");
return rtrim(s, "=");
}

return s;
Expand Down
8 changes: 0 additions & 8 deletions tests/unit/Security/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,28 +214,20 @@ function () {

$random = new Random();

$getSize = function($len) {
// Size formula: 4 *( $len / 3) and this need to be rounded up to a multiple of 4.
return (int)(round(4*($len/3))%4 === 0) ? round(4*($len/3)) : round((4*($len/3)+4/2)/4)*4;
};

$isValid = function($base64, $padding = false) {
$pattern = $padding ? "a-z0-9_=-" : "a-z0-9_-";
return (preg_match("#[^$pattern]+#i", $base64) === 0);
};

foreach ($lens as $len) {
$actual = $random->base64Safe($len);
expect(strlen($actual))->lessOrEquals($getSize($len));
expect($isValid($actual))->true();
}

$actual = $random->base64Safe();
expect(strlen($actual))->lessOrEquals($getSize(16));
expect($isValid($actual))->true();

$actual = $random->base64Safe(null, true);
expect(strlen($actual))->lessOrEquals($getSize(16));
expect($isValid($actual, true))->true();
}
);
Expand Down