Browse Source

Fixes #13407: Added URL-safe base64 encode/decode methods to `StringHelper`

tags/2.0.12
Andrew Nester 8 years ago committed by Alexander Makarov
parent
commit
953c4a8e5a
  1. 2
      framework/CHANGELOG.md
  2. 4
      framework/base/Security.php
  3. 26
      framework/helpers/BaseStringHelper.php
  4. 32
      tests/framework/helpers/StringHelperTest.php

2
framework/CHANGELOG.md

@ -25,6 +25,7 @@ Yii Framework 2 Change Log
- Enh #13576: Added support of `srcset` to `yii\helpers\Html::img()` (Kolyunya)
- Enh #13577: Implemented `yii\db\mssql\QueryBuilder::resetSequence()` (boboldehampsink)
- Enh #13582: Added tests for all `yii\db\QueryBuilder::resetSequence` implementations, fixed SQLite implementation (boboldehampsink)
- Enh #13407: Added URL-safe base64 encode/decode methods to `StringHelper` (andrewnester)
2.0.11.2 February 08, 2017
@ -170,7 +171,6 @@ Yii Framework 2 Change Log
- Enh: Added support for field `yii\console\controllers\BaseMigrateController::$migrationNamespaces` setup from CLI (schmunk42)
- Chg #11906: Updated `yii\widgets\MaskedInput` inputmask dependency to `~3.3.3` (samdark)
2.0.10 October 20, 2016
-----------------------

4
framework/base/Security.php

@ -559,9 +559,7 @@ class Security extends Component
}
$bytes = $this->generateRandomKey($length);
// '=' character(s) returned by base64_encode() are always discarded because
// they are guaranteed to be after position $length in the base64_encode() output.
return strtr(substr(base64_encode($bytes), 0, $length), '+/', '_-');
return substr(StringHelper::base64UrlEncode($bytes), 0, $length);
}
/**

26
framework/helpers/BaseStringHelper.php

@ -305,4 +305,30 @@ class BaseStringHelper
return $value;
}
/**
* Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648)
* @see https://tools.ietf.org/html/rfc4648#page-7
*
* @param string $input
* @return string
* @since 2.0.11
*/
public static function base64UrlEncode($input)
{
return strtr(base64_encode($input), '+/', '-_');
}
/**
* Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648)
* @see https://tools.ietf.org/html/rfc4648#page-7
*
* @param string $input
* @return string
* @since 2.0.11
*/
public static function base64UrlDecode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}
}

32
tests/framework/helpers/StringHelperTest.php

@ -264,4 +264,36 @@ class StringHelperTest extends TestCase
$this->assertEquals(1, StringHelper::countWords('крем-брюле'));
$this->assertEquals(1, StringHelper::countWords(' слово '));
}
/**
* @dataProvider base64UrlEncodedStringsProvider
* @param $input
* @param $base64UrlEncoded
*/
public function testBase64UrlEncode($input, $base64UrlEncoded)
{
$encoded = StringHelper::base64UrlEncode($input);
$this->assertEquals($base64UrlEncoded, $encoded);
}
/**
* @dataProvider base64UrlEncodedStringsProvider
* @param $output
* @param $base64UrlEncoded
*/
public function testBase64UrlDecode($output, $base64UrlEncoded)
{
$decoded = StringHelper::base64UrlDecode($base64UrlEncoded);
$this->assertEquals($output, $decoded);
}
public function base64UrlEncodedStringsProvider()
{
return [
['This is an encoded string', 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='],
['subjects?_d=1', 'c3ViamVjdHM_X2Q9MQ=='],
['subjects>_d=1', 'c3ViamVjdHM-X2Q9MQ=='],
['Это закодированная строка', '0K3RgtC-INC30LDQutC-0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw'],
];
}
}

Loading…
Cancel
Save