<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param string $src
* @return string
*/
- public static function decode(string $src, bool $strictPadding = false): string
+ public static function decode($src)
{
- return static::doDecode($src, false, $strictPadding);
+ return static::doDecode($src, false);
}
/**
* @param string $src
* @return string
*/
- public static function decodeUpper(string $src, bool $strictPadding = false): string
+ public static function decodeUpper($src)
{
- return static::doDecode($src, true, $strictPadding);
+ return static::doDecode($src, true);
}
/**
* @param string $src
* @return string
*/
- public static function encode(string $src): string
+ public static function encode($src)
{
return static::doEncode($src, false);
}
* @param string $src
* @return string
*/
- public static function encodeUpper(string $src): string
+ public static function encodeUpper($src)
{
return static::doEncode($src, true);
}
* @param int $src
* @return int
*/
- protected static function decode5Bits(int $src): int
+ protected static function decode5Bits($src)
{
$ret = -1;
* @param int $src
* @return int
*/
- protected static function decode5BitsUpper(int $src): int
+ protected static function decode5BitsUpper($src)
{
$ret = -1;
* @param $src
* @return string
*/
- protected static function encode5Bits(int $src): string
+ protected static function encode5Bits($src)
{
$diff = 0x61;
* @param $src
* @return string
*/
- protected static function encode5BitsUpper(int $src): string
+ protected static function encode5BitsUpper($src)
{
$diff = 0x41;
/**
* Base32 decoding
*
- * @param string $src
+ * @param $src
* @param bool $upper
- * @param bool $strictPadding
* @return string
*/
- protected static function doDecode(string $src, bool $upper = false, bool $strictPadding = false): string
+ protected static function doDecode($src, $upper = false)
{
// We do this to reduce code duplication:
$method = $upper
if ($srcLen === 0) {
return '';
}
- if ($strictPadding) {
- if (($srcLen & 7) === 0) {
- for ($j = 0; $j < 7; ++$j) {
- if ($src[$srcLen - 1] === '=') {
- $srcLen--;
- } else {
- break;
- }
+ if (($srcLen & 7) === 0) {
+ for ($j = 0; $j < 7; ++$j) {
+ if ($src[$srcLen - 1] === '=') {
+ $srcLen--;
+ } else {
+ break;
}
}
- if (($srcLen & 7) === 1) {
- throw new \RangeException(
- 'Incorrect padding'
- );
- }
- } else {
- $src = \rtrim($src, '=');
- $srcLen = Binary::safeStrlen($src);
+ }
+ if (($srcLen & 7) === 1) {
+ throw new \RangeException(
+ 'Incorrect padding'
+ );
}
$err = 0;
* @param bool $upper
* @return string
*/
- protected static function doEncode(string $src, bool $upper = false): string
+ protected static function doEncode($src, $upper = false)
{
// We do this to reduce code duplication:
$method = $upper
}
return $dest;
}
-}
+}
\ No newline at end of file
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param int $src
* @return int
*/
- protected static function decode5Bits(int $src): int
+ protected static function decode5Bits($src)
{
$ret = -1;
* @param int $src
* @return int
*/
- protected static function decode5BitsUpper(int $src): int
+ protected static function decode5BitsUpper($src)
{
$ret = -1;
* @param int $src
* @return string
*/
- protected static function encode5Bits(int $src): string
+ protected static function encode5Bits($src)
{
$src += 0x30;
* @param int $src
* @return string
*/
- protected static function encode5BitsUpper(int $src): string
+ protected static function encode5BitsUpper($src)
{
$src += 0x30;
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param string $src
* @return string
*/
- public static function encode(string $src): string
+ public static function encode($src)
{
$dest = '';
$srcLen = Binary::safeStrlen($src);
* Base64 character set "./[A-Z][a-z][0-9]"
*
* @param string $src
- * @param bool $strictPadding
* @return string|bool
* @throws \RangeException
*/
- public static function decode(string $src, bool $strictPadding = false): string
+ public static function decode($src, $strictPadding = false)
{
// Remove padding
$srcLen = Binary::safeStrlen($src);
if ($srcLen === 0) {
return '';
}
-
if ($strictPadding) {
if (($srcLen & 3) === 0) {
if ($src[$srcLen - 1] === '=') {
'Incorrect padding'
);
}
- if ($src[$srcLen - 1] === '=') {
- throw new \RangeException(
- 'Incorrect padding'
- );
- }
} else {
$src = \rtrim($src, '=');
$srcLen = Binary::safeStrlen($src);
if ($i < $srcLen) {
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
$c0 = static::decode6Bits($chunk[1]);
-
if ($i + 2 < $srcLen) {
$c1 = static::decode6Bits($chunk[2]);
$c2 = static::decode6Bits($chunk[3]);
((($c1 << 4) | ($c2 >> 2)) & 0xff)
);
$err |= ($c0 | $c1 | $c2) >> 8;
- } elseif ($i + 1 < $srcLen) {
+ } elseif($i + 1 < $srcLen) {
$c1 = static::decode6Bits($chunk[2]);
$dest .= \pack(
'C',
* @param int $src
* @return int
*/
- protected static function decode6Bits(int $src): int
+ protected static function decode6Bits($src)
{
$ret = -1;
* @param int $src
* @return string
*/
- protected static function encode6Bits(int $src): string
+ protected static function encode6Bits($src)
{
$diff = 0x41;
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param int $src
* @return int
*/
- protected static function decode6Bits(int $src): int
+ protected static function decode6Bits($src)
{
$ret = -1;
* @param int $src
* @return string
*/
- protected static function encode6Bits(int $src): string
+ protected static function encode6Bits($src)
{
$src += 0x2e;
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param int $src
* @return int
*/
- protected static function decode6Bits(int $src): int
+ protected static function decode6Bits($src)
{
$ret = -1;
* @param int $src
* @return string
*/
- protected static function encode6Bits(int $src): string
+ protected static function encode6Bits($src)
{
$src += 0x2e;
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param int $src
* @return int
*/
- protected static function decode6Bits(int $src): int
+ protected static function decode6Bits($src)
{
$ret = -1;
* @param int $src
* @return string
*/
- protected static function encode6Bits(int $src): string
+ protected static function encode6Bits($src)
{
$diff = 0x41;
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param string $str
* @return int
*/
- public static function safeStrlen(string $str): int
+ public static function safeStrlen($str)
{
if (\function_exists('mb_strlen')) {
return \mb_strlen($str, '8bit');
* @throws \TypeError
*/
public static function safeSubstr(
- string $str,
- int $start = 0,
+ $str,
+ $start = 0,
$length = null
- ): string {
+ ) {
if (\function_exists('mb_substr')) {
// mb_substr($str, 0, NULL, '8bit') returns an empty string on PHP
// 5.3, so we have to find the length ourselves.
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* Convert a binary string into a hexadecimal string without cache-timing
* leaks
*
- * @param string $binString (raw binary)
+ * @param string $bin_string (raw binary)
* @return string
*/
- public static function encode(string $binString): string;
+ public static function encode($bin_string);
/**
* Convert a binary string into a hexadecimal string without cache-timing
* leaks
*
- * @param string $encodedString
- * @param bool $strictPadding Error on invalid padding
+ * @param string $encoded_string
* @return string (raw binary)
*/
- public static function decode(string $encodedString, bool $strictPadding = false): string;
+ public static function decode($encoded_string);
}
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param $str
* @return string
*/
- public static function base32Encode(string $str): string
+ public static function base32Encode($str)
{
return Base32::encode($str);
}
* @param $str
* @return string
*/
- public static function base32EncodeUpper(string $str): string
+ public static function base32EncodeUpper($str)
{
return Base32::encodeUpper($str);
}
* @param $str
* @return string
*/
- public static function base32Decode(string $str): string
+ public static function base32Decode($str)
{
return Base32::decode($str);
}
* @param $str
* @return string
*/
- public static function base32DecodeUpper(string $str): string
+ public static function base32DecodeUpper($str)
{
return Base32::decodeUpper($str);
}
* @param $str
* @return string
*/
- public static function base32HexEncode(string $str): string
+ public static function base32HexEncode($str)
{
return Base32Hex::encode($str);
}
* @param $str
* @return string
*/
- public static function base32HexEncodeUpper(string $str): string
+ public static function base32HexEncodeUpper($str)
{
return Base32Hex::encodeUpper($str);
}
* @param $str
* @return string
*/
- public static function base32HexDecode(string $str): string
+ public static function base32HexDecode($str)
{
return Base32Hex::decode($str);
}
* @param $str
* @return string
*/
- public static function base32HexDecodeUpper(string $str): string
+ public static function base32HexDecodeUpper($str)
{
return Base32Hex::decodeUpper($str);
}
* @param $str
* @return string
*/
- public static function base64Encode(string $str): string
+ public static function base64Encode($str)
{
return Base64::encode($str);
}
* @param $str
* @return string
*/
- public static function base64Decode(string $str): string
+ public static function base64Decode($str)
{
return Base64::decode($str);
}
* @param $src
* @return string
*/
- public static function base64EncodeDotSlash(string $str): string
+ public static function base64EncodeDotSlash($src)
{
- return Base64DotSlash::encode($str);
+ return Base64DotSlash::encode($src);
}
/**
* @return bool|string
* @throws \RangeException
*/
- public static function base64DecodeDotSlash(string $str): string
+ public static function base64DecodeDotSlash($src)
{
- return Base64DotSlash::decode($str);
+ return Base64DotSlash::decode($src);
}
/**
* @param $src
* @return string
*/
- public static function base64EncodeDotSlashOrdered(string $str): string
+ public static function base64EncodeDotSlashOrdered($src)
{
- return Base64DotSlashOrdered::encode($str);
+ return Base64DotSlashOrdered::encode($src);
}
/**
* @return bool|string
* @throws \RangeException
*/
- public static function base64DecodeDotSlashOrdered(string $str): string
+ public static function base64DecodeDotSlashOrdered($src)
{
- return Base64DotSlashOrdered::decode($str);
+ return Base64DotSlashOrdered::decode($src);
}
/**
* @param string $bin_string (raw binary)
* @return string
*/
- public static function hexEncode(string $bin_string): string
+ public static function hexEncode($bin_string)
{
return Hex::encode($bin_string);
}
* @return string (raw binary)
* @throws \RangeException
*/
- public static function hexDecode(string $hex_string): string
+ public static function hexDecode($hex_string)
{
return Hex::decode($hex_string);
}
* @param string $bin_string (raw binary)
* @return string
*/
- public static function hexEncodeUpper(string $bin_string): string
+ public static function hexEncodeUpper($bin_string)
{
return Hex::encodeUpper($bin_string);
}
* @param string $bin_string (raw binary)
* @return string
*/
- public static function hexDecodeUpper(string $bin_string): string
+ public static function hexDecodeUpper($bin_string)
{
return Hex::decode($bin_string);
}
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param string $bin_string (raw binary)
* @return string
*/
- public static function encode(string $bin_string): string
+ public static function encode($bin_string)
{
$hex = '';
$len = Binary::safeStrlen($bin_string);
* @param string $bin_string (raw binary)
* @return string
*/
- public static function encodeUpper(string $bin_string): string
+ public static function encodeUpper($bin_string)
{
$hex = '';
$len = Binary::safeStrlen($bin_string);
* leaks
*
* @param string $hex_string
- * @param bool $strictPadding
* @return string (raw binary)
* @throws \RangeException
*/
- public static function decode(string $hexString, bool $strictPadding = false): string
+ public static function decode($hex_string)
{
$hex_pos = 0;
$bin = '';
$c_acc = 0;
- $hex_len = Binary::safeStrlen($hexString);
+ $hex_len = Binary::safeStrlen($hex_string);
$state = 0;
if (($hex_len & 1) !== 0) {
- if ($strictPadding) {
- throw new \RangeException(
- 'Expected an even number of hexadecimal characters'
- );
- } else {
- $hexString = '0' . $hexString;
- ++$hex_len;
- }
+ throw new \RangeException(
+ 'Expected an even number of hexadecimal characters'
+ );
}
- $chunk = \unpack('C*', $hexString);
+ $chunk = \unpack('C*', $hex_string);
while ($hex_pos < $hex_len) {
++$hex_pos;
$c = $chunk[$hex_pos];
<?php
-declare(strict_types=1);
namespace ParagonIE\ConstantTime;
/**
* @param string $str
* @return string
*/
- public function base64Encode(string $str): string
+ public function base64Encode($str)
{
return Base64::encode($str);
}
* @param string $str
* @return string
*/
- public function base64Decode(string $str): string
+ public function base64Decode($str)
{
- return Base64::decode($str, true);
+ return Base64::decode($str);
}
/**
* @param string $str
* @return string
*/
- public function base64UrlSafeEncode(string $str): string
+ public function base64UrlSafeEncode($str)
{
return Base64UrlSafe::encode($str);
}
* @param string $str
* @return string
*/
- public function base64UrlSafeDecode(string $str): string
+ public function base64UrlSafeDecode($str)
{
- return Base64UrlSafe::decode($str, true);
+ return Base64UrlSafe::decode($str);
}
/**
* @param string $str
* @return string
*/
- public function base32Encode(string $str): string
+ public function base32Encode($str)
{
return Base32::encodeUpper($str);
}
* @param string $str
* @return string
*/
- public function base32Decode(string $str): string
+ public function base32Decode($str)
{
- return Base32::decodeUpper($str, true);
+ return Base32::decodeUpper($str);
}
/**
* @param string $str
* @return string
*/
- public function base32HexEncode(string $str): string
+ public function base32HexEncode($str)
{
return Base32::encodeUpper($str);
}
* @param string $str
* @return string
*/
- public function base32HexDecode(string $str): string
+ public function base32HexDecode($str)
{
- return Base32::decodeUpper($str, true);
+ return Base32::decodeUpper($str);
}
/**
* @param string $str
* @return string
*/
- public function base16Encode(string $str): string
+ public function base16Encode($str)
{
return Hex::encodeUpper($str);
}
* @param string $str
* @return string
*/
- public function base16Decode(string $str): string
+ public function base16Decode($str)
{
- return Hex::decode($str, true);
+ return Hex::decode($str);
}
}
\ No newline at end of file
--- /dev/null
+# Constant-Time Encoding
+
+[![Build Status](https://travis-ci.org/paragonie/constant_time_encoding.svg?branch=v1.x)](https://travis-ci.org/paragonie/constant_time_encoding)
+
+Based on the [constant-time base64 implementation made by Steve "Sc00bz" Thomas](https://github.com/Sc00bz/ConstTimeEncoding),
+this library aims to offer character encoding functions that do not leak
+information about what you are encoding/decoding via processor cache
+misses. Further reading on [cache-timing attacks](http://blog.ircmaxell.com/2014/11/its-all-about-time.html).
+
+Our fork offers the following enchancements:
+
+* `mbstring.func_overload` resistance
+* Unit tests
+* Composer- and Packagist-ready
+* Base16 encoding
+* Base32 encoding
+* Uses `pack()` and `unpack()` instead of `chr()` and `ord()`
+
+## PHP Version Requirements
+
+This library should work on any [supported version of PHP](https://secure.php.net/supported-versions.php).
+It *may* work on earlier versions, but we **do not** guarantee it. If it
+doesn't, we **will not** fix it to work on earlier versions of PHP.
+
+## How to Install
+
+```sh
+composer require paragonie/constant_time_encoding
+```
+
+## How to Use
+
+```php
+use \ParagonIE\ConstantTime\Encoding;
+
+// possibly (if applicable):
+// require 'vendor/autoload.php';
+
+$data = random_bytes(32);
+echo Encoding::base64Encode($data), "\n";
+echo Encoding::base32EncodeUpper($data), "\n";
+echo Encoding::base32Encode($data), "\n";
+echo Encoding::hexEncode($data), "\n";
+echo Encoding::hexEncodeUpper($data), "\n";
+```
+
+Example output:
+
+```
+1VilPkeVqirlPifk5scbzcTTbMT2clp+Zkyv9VFFasE=
+2VMKKPSHSWVCVZJ6E7SONRY3ZXCNG3GE6ZZFU7TGJSX7KUKFNLAQ====
+2vmkkpshswvcvzj6e7sonry3zxcng3ge6zzfu7tgjsx7kukfnlaq====
+d558a53e4795aa2ae53e27e4e6c71bcdc4d36cc4f6725a7e664caff551456ac1
+D558A53E4795AA2AE53E27E4E6C71BDCC4D36CC4F6725A7E664CAFF551456AC1
+```
+
+If you only need a particular variant, you can just reference the
+required class like so:
+
+```php
+use \ParagonIE\ConstantTime\Base64;
+use \ParagonIE\ConstantTime\Base32;
+
+$data = random_bytes(32);
+echo Base64::encode($data), "\n";
+echo Base32::encode($data), "\n";
+```
+
+Example output:
+
+```
+1VilPkeVqirlPifk5scbzcTTbMT2clp+Zkyv9VFFasE=
+2vmkkpshswvcvzj6e7sonry3zxcng3ge6zzfu7tgjsx7kukfnlaq====
+```
\ No newline at end of file