upgrade idiorm to php8.1-patched version (aaronpk/idiorm)
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
.idea/
|
||||
vendor/
|
||||
@@ -1,24 +0,0 @@
|
||||
language: php
|
||||
sudo: false
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
include:
|
||||
- php: "7.1"
|
||||
- php: "7.2"
|
||||
- php: "7.3"
|
||||
- php: "7.4"
|
||||
- php: "8.0"
|
||||
- php: "nightly"
|
||||
allow_failures:
|
||||
- php: "nightly"
|
||||
- php: "7.4"
|
||||
- php: "8.0"
|
||||
|
||||
install:
|
||||
- composer self-update
|
||||
- composer update
|
||||
|
||||
script:
|
||||
- vendor/bin/phpunit
|
||||
- vendor/bin/psalm
|
||||
@@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 - 2020 Paragon Initiative Enterprises
|
||||
Copyright (c) 2016 - 2022 Paragon Initiative Enterprises
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Constant-Time Encoding
|
||||
|
||||
[](https://travis-ci.org/paragonie/constant_time_encoding)
|
||||
[](https://github.com/paragonie/constant_time_encoding/actions)
|
||||
[](https://packagist.org/packages/paragonie/constant_time_encoding)
|
||||
[](https://packagist.org/packages/paragonie/constant_time_encoding)
|
||||
[](https://packagist.org/packages/paragonie/constant_time_encoding)
|
||||
@@ -36,7 +36,7 @@ composer require paragonie/constant_time_encoding
|
||||
## How to Use
|
||||
|
||||
```php
|
||||
use \ParagonIE\ConstantTime\Encoding;
|
||||
use ParagonIE\ConstantTime\Encoding;
|
||||
|
||||
// possibly (if applicable):
|
||||
// require 'vendor/autoload.php';
|
||||
@@ -63,8 +63,8 @@ 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;
|
||||
use ParagonIE\ConstantTime\Base64;
|
||||
use ParagonIE\ConstantTime\Base32;
|
||||
|
||||
$data = random_bytes(32);
|
||||
echo Base64::encode($data), "\n";
|
||||
|
||||
@@ -47,5 +47,10 @@
|
||||
"psr-4": {
|
||||
"ParagonIE\\ConstantTime\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"ParagonIE\\ConstantTime\\Tests\\": "tests/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="true" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
|
||||
<coverage processUncoveredFiles="true">
|
||||
<include>
|
||||
<directory suffix=".php">./src</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
<testsuites>
|
||||
<testsuite name="Constant Time Encoding Test Suite">
|
||||
<directory suffix="Test.php">./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<psalm
|
||||
useDocblockTypes="true"
|
||||
totallyTyped="true"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="src" />
|
||||
</projectFiles>
|
||||
</psalm>
|
||||
@@ -2,8 +2,12 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use RangeException;
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -60,20 +64,20 @@ abstract class Base32 implements EncoderInterface
|
||||
/**
|
||||
* Encode into Base32 (RFC 4648)
|
||||
*
|
||||
* @param string $src
|
||||
* @param string $binString
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encode(string $src): string
|
||||
public static function encode(string $binString): string
|
||||
{
|
||||
return static::doEncode($src, false, true);
|
||||
return static::doEncode($binString, false, true);
|
||||
}
|
||||
/**
|
||||
* Encode into Base32 (RFC 4648)
|
||||
*
|
||||
* @param string $src
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encodeUnpadded(string $src): string
|
||||
{
|
||||
@@ -85,7 +89,7 @@ abstract class Base32 implements EncoderInterface
|
||||
*
|
||||
* @param string $src
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encodeUpper(string $src): string
|
||||
{
|
||||
@@ -97,7 +101,7 @@ abstract class Base32 implements EncoderInterface
|
||||
*
|
||||
* @param string $src
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encodeUpperUnpadded(string $src): string
|
||||
{
|
||||
@@ -182,6 +186,32 @@ abstract class Base32 implements EncoderInterface
|
||||
return \pack('C', $src + $diff);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $encodedString
|
||||
* @param bool $upper
|
||||
* @return string
|
||||
*/
|
||||
public static function decodeNoPadding(string $encodedString, bool $upper = false): string
|
||||
{
|
||||
$srcLen = Binary::safeStrlen($encodedString);
|
||||
if ($srcLen === 0) {
|
||||
return '';
|
||||
}
|
||||
if (($srcLen & 7) === 0) {
|
||||
for ($j = 0; $j < 7 && $j < $srcLen; ++$j) {
|
||||
if ($encodedString[$srcLen - $j - 1] === '=') {
|
||||
throw new InvalidArgumentException(
|
||||
"decodeNoPadding() doesn't tolerate padding"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return static::doDecode(
|
||||
$encodedString,
|
||||
$upper,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Base32 decoding
|
||||
@@ -190,11 +220,15 @@ abstract class Base32 implements EncoderInterface
|
||||
* @param bool $upper
|
||||
* @param bool $strictPadding
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
* @psalm-suppress RedundantCondition
|
||||
*/
|
||||
protected static function doDecode(string $src, bool $upper = false, bool $strictPadding = false): string
|
||||
{
|
||||
protected static function doDecode(
|
||||
string $src,
|
||||
bool $upper = false,
|
||||
bool $strictPadding = false
|
||||
): string {
|
||||
// We do this to reduce code duplication:
|
||||
$method = $upper
|
||||
? 'decode5BitsUpper'
|
||||
@@ -216,7 +250,7 @@ abstract class Base32 implements EncoderInterface
|
||||
}
|
||||
}
|
||||
if (($srcLen & 7) === 1) {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Incorrect padding'
|
||||
);
|
||||
}
|
||||
@@ -287,6 +321,9 @@ abstract class Base32 implements EncoderInterface
|
||||
(($c4 << 7) | ($c5 << 2) | ($c6 >> 3)) & 0xff
|
||||
);
|
||||
$err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6) >> 8;
|
||||
if ($strictPadding) {
|
||||
$err |= ($c6 << 5) & 0xff;
|
||||
}
|
||||
} elseif ($i + 5 < $srcLen) {
|
||||
/** @var int $c1 */
|
||||
$c1 = static::$method($chunk[2]);
|
||||
@@ -324,6 +361,9 @@ abstract class Base32 implements EncoderInterface
|
||||
(($c3 << 4) | ($c4 >> 1) ) & 0xff
|
||||
);
|
||||
$err |= ($c0 | $c1 | $c2 | $c3 | $c4) >> 8;
|
||||
if ($strictPadding) {
|
||||
$err |= ($c4 << 7) & 0xff;
|
||||
}
|
||||
} elseif ($i + 3 < $srcLen) {
|
||||
/** @var int $c1 */
|
||||
$c1 = static::$method($chunk[2]);
|
||||
@@ -338,6 +378,9 @@ abstract class Base32 implements EncoderInterface
|
||||
(($c1 << 6) | ($c2 << 1) | ($c3 >> 4)) & 0xff
|
||||
);
|
||||
$err |= ($c0 | $c1 | $c2 | $c3) >> 8;
|
||||
if ($strictPadding) {
|
||||
$err |= ($c3 << 4) & 0xff;
|
||||
}
|
||||
} elseif ($i + 2 < $srcLen) {
|
||||
/** @var int $c1 */
|
||||
$c1 = static::$method($chunk[2]);
|
||||
@@ -350,6 +393,9 @@ abstract class Base32 implements EncoderInterface
|
||||
(($c1 << 6) | ($c2 << 1) ) & 0xff
|
||||
);
|
||||
$err |= ($c0 | $c1 | $c2) >> 8;
|
||||
if ($strictPadding) {
|
||||
$err |= ($c2 << 6) & 0xff;
|
||||
}
|
||||
} elseif ($i + 1 < $srcLen) {
|
||||
/** @var int $c1 */
|
||||
$c1 = static::$method($chunk[2]);
|
||||
@@ -359,6 +405,9 @@ abstract class Base32 implements EncoderInterface
|
||||
(($c0 << 3) | ($c1 >> 2) ) & 0xff
|
||||
);
|
||||
$err |= ($c0 | $c1) >> 8;
|
||||
if ($strictPadding) {
|
||||
$err |= ($c1 << 6) & 0xff;
|
||||
}
|
||||
} else {
|
||||
$dest .= \pack(
|
||||
'C',
|
||||
@@ -367,10 +416,9 @@ abstract class Base32 implements EncoderInterface
|
||||
$err |= ($c0) >> 8;
|
||||
}
|
||||
}
|
||||
/** @var bool $check */
|
||||
$check = ($err === 0);
|
||||
if (!$check) {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Base32::doDecode() only expects characters in the correct base32 alphabet'
|
||||
);
|
||||
}
|
||||
@@ -384,7 +432,7 @@ abstract class Base32 implements EncoderInterface
|
||||
* @param bool $upper
|
||||
* @param bool $pad
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
protected static function doEncode(string $src, bool $upper = false, $pad = true): string
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use RangeException;
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -38,13 +42,14 @@ abstract class Base64 implements EncoderInterface
|
||||
*
|
||||
* Base64 character set "[A-Z][a-z][0-9]+/"
|
||||
*
|
||||
* @param string $src
|
||||
* @param string $binString
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encode(string $src): string
|
||||
public static function encode(string $binString): string
|
||||
{
|
||||
return static::doEncode($src, true);
|
||||
return static::doEncode($binString, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +59,8 @@ abstract class Base64 implements EncoderInterface
|
||||
*
|
||||
* @param string $src
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encodeUnpadded(string $src): string
|
||||
{
|
||||
@@ -65,7 +71,8 @@ abstract class Base64 implements EncoderInterface
|
||||
* @param string $src
|
||||
* @param bool $pad Include = padding?
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
protected static function doEncode(string $src, bool $pad = true): string
|
||||
{
|
||||
@@ -119,8 +126,9 @@ abstract class Base64 implements EncoderInterface
|
||||
* @param string $encodedString
|
||||
* @param bool $strictPadding
|
||||
* @return string
|
||||
* @throws \RangeException
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws RangeException
|
||||
* @throws TypeError
|
||||
* @psalm-suppress RedundantCondition
|
||||
*/
|
||||
public static function decode(string $encodedString, bool $strictPadding = false): string
|
||||
@@ -141,12 +149,12 @@ abstract class Base64 implements EncoderInterface
|
||||
}
|
||||
}
|
||||
if (($srcLen & 3) === 1) {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Incorrect padding'
|
||||
);
|
||||
}
|
||||
if ($encodedString[$srcLen - 1] === '=') {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Incorrect padding'
|
||||
);
|
||||
}
|
||||
@@ -189,6 +197,9 @@ abstract class Base64 implements EncoderInterface
|
||||
((($c1 << 4) | ($c2 >> 2)) & 0xff)
|
||||
);
|
||||
$err |= ($c0 | $c1 | $c2) >> 8;
|
||||
if ($strictPadding) {
|
||||
$err |= ($c2 << 6) & 0xff;
|
||||
}
|
||||
} elseif ($i + 1 < $srcLen) {
|
||||
$c1 = static::decode6Bits($chunk[2]);
|
||||
$dest .= \pack(
|
||||
@@ -196,20 +207,52 @@ abstract class Base64 implements EncoderInterface
|
||||
((($c0 << 2) | ($c1 >> 4)) & 0xff)
|
||||
);
|
||||
$err |= ($c0 | $c1) >> 8;
|
||||
} elseif ($i < $srcLen && $strictPadding) {
|
||||
if ($strictPadding) {
|
||||
$err |= ($c1 << 4) & 0xff;
|
||||
}
|
||||
} elseif ($strictPadding) {
|
||||
$err |= 1;
|
||||
}
|
||||
}
|
||||
/** @var bool $check */
|
||||
$check = ($err === 0);
|
||||
if (!$check) {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Base64::decode() only expects characters in the correct base64 alphabet'
|
||||
);
|
||||
}
|
||||
return $dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $encodedString
|
||||
* @return string
|
||||
*/
|
||||
public static function decodeNoPadding(string $encodedString): string
|
||||
{
|
||||
$srcLen = Binary::safeStrlen($encodedString);
|
||||
if ($srcLen === 0) {
|
||||
return '';
|
||||
}
|
||||
if (($srcLen & 3) === 0) {
|
||||
if ($encodedString[$srcLen - 1] === '=') {
|
||||
throw new InvalidArgumentException(
|
||||
"decodeNoPadding() doesn't tolerate padding"
|
||||
);
|
||||
}
|
||||
if (($srcLen & 3) > 1) {
|
||||
if ($encodedString[$srcLen - 2] === '=') {
|
||||
throw new InvalidArgumentException(
|
||||
"decodeNoPadding() doesn't tolerate padding"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return static::decode(
|
||||
$encodedString,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses bitwise operators instead of table-lookups to turn 6-bit integers
|
||||
* into 8-bit integers.
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -46,6 +48,8 @@ abstract class Binary
|
||||
public static function safeStrlen(string $str): int
|
||||
{
|
||||
if (\function_exists('mb_strlen')) {
|
||||
// mb_strlen in PHP 7.x can return false.
|
||||
/** @psalm-suppress RedundantCast */
|
||||
return (int) \mb_strlen($str, '8bit');
|
||||
} else {
|
||||
return \strlen($str);
|
||||
@@ -60,9 +64,10 @@ abstract class Binary
|
||||
* @staticvar boolean $exists
|
||||
* @param string $str
|
||||
* @param int $start
|
||||
* @param int $length
|
||||
* @param ?int $length
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function safeSubstr(
|
||||
string $str,
|
||||
|
||||
@@ -3,7 +3,7 @@ declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -36,7 +38,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32Encode(string $str): string
|
||||
{
|
||||
@@ -48,7 +50,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32EncodeUpper(string $str): string
|
||||
{
|
||||
@@ -60,7 +62,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32Decode(string $str): string
|
||||
{
|
||||
@@ -72,7 +74,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32DecodeUpper(string $str): string
|
||||
{
|
||||
@@ -84,7 +86,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexEncode(string $str): string
|
||||
{
|
||||
@@ -96,7 +98,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexEncodeUpper(string $str): string
|
||||
{
|
||||
@@ -108,7 +110,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexDecode(string $str): string
|
||||
{
|
||||
@@ -120,7 +122,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexDecodeUpper(string $str): string
|
||||
{
|
||||
@@ -132,7 +134,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64Encode(string $str): string
|
||||
{
|
||||
@@ -144,7 +146,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64Decode(string $str): string
|
||||
{
|
||||
@@ -157,7 +159,7 @@ abstract class Encoding
|
||||
* Base64 character set "./[A-Z][a-z][0-9]"
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64EncodeDotSlash(string $str): string
|
||||
{
|
||||
@@ -172,7 +174,7 @@ abstract class Encoding
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \RangeException
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64DecodeDotSlash(string $str): string
|
||||
{
|
||||
@@ -185,7 +187,7 @@ abstract class Encoding
|
||||
* Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64EncodeDotSlashOrdered(string $str): string
|
||||
{
|
||||
@@ -200,7 +202,7 @@ abstract class Encoding
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \RangeException
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64DecodeDotSlashOrdered(string $str): string
|
||||
{
|
||||
@@ -213,7 +215,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $bin_string (raw binary)
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hexEncode(string $bin_string): string
|
||||
{
|
||||
@@ -239,7 +241,7 @@ abstract class Encoding
|
||||
*
|
||||
* @param string $bin_string (raw binary)
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function hexEncodeUpper(string $bin_string): string
|
||||
{
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
use RangeException;
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -37,22 +40,19 @@ abstract class Hex implements EncoderInterface
|
||||
*
|
||||
* @param string $binString (raw binary)
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encode(string $binString): string
|
||||
{
|
||||
/** @var string $hex */
|
||||
$hex = '';
|
||||
$len = Binary::safeStrlen($binString);
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
/** @var array<int, int> $chunk */
|
||||
$chunk = \unpack('C', Binary::safeSubstr($binString, $i, 1));
|
||||
/** @var int $c */
|
||||
$chunk = \unpack('C', $binString[$i]);
|
||||
$c = $chunk[1] & 0xf;
|
||||
/** @var int $b */
|
||||
$b = $chunk[1] >> 4;
|
||||
|
||||
$hex .= pack(
|
||||
$hex .= \pack(
|
||||
'CC',
|
||||
(87 + $b + ((($b - 10) >> 8) & ~38)),
|
||||
(87 + $c + ((($c - 10) >> 8) & ~38))
|
||||
@@ -67,24 +67,20 @@ abstract class Hex implements EncoderInterface
|
||||
*
|
||||
* @param string $binString (raw binary)
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function encodeUpper(string $binString): string
|
||||
{
|
||||
/** @var string $hex */
|
||||
$hex = '';
|
||||
/** @var int $len */
|
||||
$len = Binary::safeStrlen($binString);
|
||||
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
/** @var array<int, int> $chunk */
|
||||
$chunk = \unpack('C', Binary::safeSubstr($binString, $i, 2));
|
||||
/** @var int $c */
|
||||
$chunk = \unpack('C', $binString[$i]);
|
||||
$c = $chunk[1] & 0xf;
|
||||
/** @var int $b */
|
||||
$b = $chunk[1] >> 4;
|
||||
|
||||
$hex .= pack(
|
||||
$hex .= \pack(
|
||||
'CC',
|
||||
(55 + $b + ((($b - 10) >> 8) & ~6)),
|
||||
(55 + $c + ((($c - 10) >> 8) & ~6))
|
||||
@@ -100,23 +96,20 @@ abstract class Hex implements EncoderInterface
|
||||
* @param string $encodedString
|
||||
* @param bool $strictPadding
|
||||
* @return string (raw binary)
|
||||
* @throws \RangeException
|
||||
* @throws RangeException
|
||||
*/
|
||||
public static function decode(string $encodedString, bool $strictPadding = false): string
|
||||
{
|
||||
/** @var int $hex_pos */
|
||||
public static function decode(
|
||||
string $encodedString,
|
||||
bool $strictPadding = false
|
||||
): string {
|
||||
$hex_pos = 0;
|
||||
/** @var string $bin */
|
||||
$bin = '';
|
||||
/** @var int $c_acc */
|
||||
$c_acc = 0;
|
||||
/** @var int $hex_len */
|
||||
$hex_len = Binary::safeStrlen($encodedString);
|
||||
/** @var int $state */
|
||||
$state = 0;
|
||||
if (($hex_len & 1) !== 0) {
|
||||
if ($strictPadding) {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Expected an even number of hexadecimal characters'
|
||||
);
|
||||
} else {
|
||||
@@ -129,23 +122,17 @@ abstract class Hex implements EncoderInterface
|
||||
$chunk = \unpack('C*', $encodedString);
|
||||
while ($hex_pos < $hex_len) {
|
||||
++$hex_pos;
|
||||
/** @var int $c */
|
||||
$c = $chunk[$hex_pos];
|
||||
/** @var int $c_num */
|
||||
$c_num = $c ^ 48;
|
||||
/** @var int $c_num0 */
|
||||
$c_num0 = ($c_num - 10) >> 8;
|
||||
/** @var int $c_alpha */
|
||||
$c_alpha = ($c & ~32) - 55;
|
||||
/** @var int $c_alpha0 */
|
||||
$c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
|
||||
|
||||
if (($c_num0 | $c_alpha0) === 0) {
|
||||
throw new \RangeException(
|
||||
throw new RangeException(
|
||||
'Expected hexadecimal character'
|
||||
);
|
||||
}
|
||||
/** @var int $c_val */
|
||||
$c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
|
||||
if ($state === 0) {
|
||||
$c_acc = $c_val * 16;
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
declare(strict_types=1);
|
||||
namespace ParagonIE\ConstantTime;
|
||||
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Copyright (c) 2016 - 2018 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
|
||||
* Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -41,7 +43,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64Encode(string $str): string
|
||||
{
|
||||
@@ -55,7 +58,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64Decode(string $str): string
|
||||
{
|
||||
@@ -69,7 +73,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64UrlSafeEncode(string $str): string
|
||||
{
|
||||
@@ -83,7 +88,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base64UrlSafeDecode(string $str): string
|
||||
{
|
||||
@@ -97,7 +103,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32Encode(string $str): string
|
||||
{
|
||||
@@ -111,7 +118,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32Decode(string $str): string
|
||||
{
|
||||
@@ -125,7 +133,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexEncode(string $str): string
|
||||
{
|
||||
@@ -139,7 +148,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base32HexDecode(string $str): string
|
||||
{
|
||||
@@ -153,7 +163,8 @@ abstract class RFC4648
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
* @throws \TypeError
|
||||
*
|
||||
* @throws TypeError
|
||||
*/
|
||||
public static function base16Encode(string $str): string
|
||||
{
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
use \ParagonIE\ConstantTime\Base32Hex;
|
||||
|
||||
class Base32HexTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Base32Hex::encode()
|
||||
* @covers Base32Hex::decode()
|
||||
* @covers Base32Hex::encodeUpper()
|
||||
* @covers Base32Hex::decodeUpper()
|
||||
*/
|
||||
public function testRandom()
|
||||
{
|
||||
for ($i = 1; $i < 32; ++$i) {
|
||||
for ($j = 0; $j < 50; ++$j) {
|
||||
$random = \random_bytes($i);
|
||||
|
||||
$enc = Base32Hex::encode($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base32Hex::decode($enc)
|
||||
);
|
||||
$unpadded = \rtrim($enc, '=');
|
||||
$this->assertSame(
|
||||
$unpadded,
|
||||
Base32Hex::encodeUnpadded($random)
|
||||
);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base32Hex::decode($unpadded)
|
||||
);
|
||||
|
||||
$enc = Base32Hex::encodeUpper($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base32Hex::decodeUpper($enc)
|
||||
); $unpadded = \rtrim($enc, '=');
|
||||
$this->assertSame(
|
||||
$unpadded,
|
||||
Base32Hex::encodeUpperUnpadded($random)
|
||||
);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base32Hex::decodeUpper($unpadded)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
use \ParagonIE\ConstantTime\Base32;
|
||||
|
||||
class Base32Test extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Base32::encode()
|
||||
* @covers Base32::decode()
|
||||
* @covers Base32::encodeUpper()
|
||||
* @covers Base32::decodeUpper()
|
||||
*/
|
||||
public function testRandom()
|
||||
{
|
||||
for ($i = 1; $i < 32; ++$i) {
|
||||
for ($j = 0; $j < 50; ++$j) {
|
||||
$random = \random_bytes($i);
|
||||
|
||||
$enc = Base32::encode($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base32::decode($enc)
|
||||
);
|
||||
$unpadded = \rtrim($enc, '=');
|
||||
$this->assertSame(
|
||||
$unpadded,
|
||||
Base32::encodeUnpadded($random)
|
||||
);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base32::decode($unpadded)
|
||||
);
|
||||
|
||||
$enc = Base32::encodeUpper($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base32::decodeUpper($enc)
|
||||
);
|
||||
$unpadded = \rtrim($enc, '=');
|
||||
$this->assertSame(
|
||||
$unpadded,
|
||||
Base32::encodeUpperUnpadded($random)
|
||||
);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base32::decodeUpper($unpadded)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
use \ParagonIE\ConstantTime\Base64DotSlashOrdered;
|
||||
|
||||
class Base64DotSlashOrderedTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Base64DotSlashOrdered::encode()
|
||||
* @covers Base64DotSlashOrdered::decode()
|
||||
*/
|
||||
public function testRandom()
|
||||
{
|
||||
for ($i = 1; $i < 32; ++$i) {
|
||||
for ($j = 0; $j < 50; ++$j) {
|
||||
$random = \random_bytes($i);
|
||||
|
||||
$enc = Base64DotSlashOrdered::encode($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64DotSlashOrdered::decode($enc)
|
||||
);
|
||||
|
||||
$unpadded = \rtrim($enc, '=');
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64DotSlashOrdered::decode($unpadded)
|
||||
);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64DotSlashOrdered::decode($unpadded)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
use \ParagonIE\ConstantTime\Base64DotSlash;
|
||||
|
||||
class Base64DotSlashTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Base64DotSlash::encode()
|
||||
* @covers Base64DotSlash::decode()
|
||||
*/
|
||||
public function testRandom()
|
||||
{
|
||||
for ($i = 1; $i < 32; ++$i) {
|
||||
for ($j = 0; $j < 50; ++$j) {
|
||||
$random = \random_bytes($i);
|
||||
|
||||
$enc = Base64DotSlash::encode($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64DotSlash::decode($enc)
|
||||
);
|
||||
|
||||
$unpadded = \rtrim($enc, '=');
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64DotSlash::decode($unpadded)
|
||||
);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64DotSlash::decode($unpadded)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
<?php
|
||||
use \ParagonIE\ConstantTime\Base64;
|
||||
|
||||
class Base64Test extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Base64::encode()
|
||||
* @covers Base64::decode()
|
||||
*/
|
||||
public function testRandom()
|
||||
{
|
||||
for ($i = 1; $i < 32; ++$i) {
|
||||
for ($j = 0; $j < 50; ++$j) {
|
||||
$random = \random_bytes($i);
|
||||
|
||||
$enc = Base64::encode($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64::decode($enc)
|
||||
);
|
||||
$this->assertSame(
|
||||
\base64_encode($random),
|
||||
$enc
|
||||
);
|
||||
|
||||
$unpadded = \rtrim($enc, '=');
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64::decode($unpadded)
|
||||
);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64::decode($unpadded)
|
||||
);
|
||||
}
|
||||
}
|
||||
$str = 'MIIFzzCCBLegAwIBAgIDAfdlMA0GCSqGSIb3DQEBBQUAMHMxCzAJBgNVBAYTAlBM' .
|
||||
'MSgwJgYDVQQKDB9LcmFqb3dhIEl6YmEgUm96bGljemVuaW93YSBTLkEuMSQwIgYDVQQ' .
|
||||
'DDBtDT1BFIFNaQUZJUiAtIEt3YWxpZmlrb3dhbnkxFDASBgNVBAUTC05yIHdwaXN1Oi' .
|
||||
'A2MB4XDTExMTEwOTA2MDAwMFoXDTEzMTEwOTA2MDAwMFowgdkxCzAJBgNVBAYTAlBMM' .
|
||||
'RwwGgYDVQQKDBNVcnrEhWQgTWlhc3RhIEdkeW5pMRswGQYDVQQFExJQRVNFTDogNjEw' .
|
||||
'NjA2MDMxMTgxGTAXBgNVBAMMEEplcnp5IFByemV3b3Jza2kxTzBNBgNVBBAwRgwiQWw' .
|
||||
'uIE1hcnN6YcWCa2EgUGnFgnN1ZHNraWVnbyA1Mi81NAwNODEtMzgyIEdkeW5pYQwGUG' .
|
||||
'9sc2thDAlwb21vcnNraWUxDjAMBgNVBCoMBUplcnp5MRMwEQYDVQQEDApQcnpld29yc' .
|
||||
'2tpMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCMm5vjGqHPthJCMqKpqssSISRo' .
|
||||
's0PYDTcEQzyyurfX67EJWKtZj6HNwuDMEGJ02iBNZfjUl7r8dIi28bSKhNlsfycXZKY' .
|
||||
'RcIjp0+r5RqtR2auo9GQ6veKb61DEAGIqaR+uLLcJVTHCu0w9oXLGbRlGth5eNoj03C' .
|
||||
'xXVAH2IfhbNwIDAQABo4IChzCCAoMwDAYDVR0TAQH/BAIwADCCAUgGA1UdIAEB/wSCA' .
|
||||
'TwwggE4MIIBNAYJKoRoAYb3IwEBMIIBJTCB3QYIKwYBBQUHAgIwgdAMgc1EZWtsYXJh' .
|
||||
'Y2phIHRhIGplc3Qgb8Wbd2lhZGN6ZW5pZW0gd3lkYXdjeSwgxbxlIHRlbiBjZXJ0eWZ' .
|
||||
'pa2F0IHpvc3RhxYIgd3lkYW55IGpha28gY2VydHlmaWthdCBrd2FsaWZpa293YW55IH' .
|
||||
'pnb2RuaWUgeiB3eW1hZ2FuaWFtaSB1c3Rhd3kgbyBwb2RwaXNpZSBlbGVrdHJvbmlje' .
|
||||
'm55bSBvcmF6IHRvd2FyenlzesSFY3ltaSBqZWogcm96cG9yesSFZHplbmlhbWkuMEMG' .
|
||||
'CCsGAQUFBwIBFjdodHRwOi8vd3d3Lmtpci5jb20ucGwvY2VydHlmaWthY2phX2tsdWN' .
|
||||
'6eS9wb2xpdHlrYS5odG1sMAkGA1UdCQQCMAAwIQYDVR0RBBowGIEWai5wcnpld29yc2' .
|
||||
'tpQGdkeW5pYS5wbDAOBgNVHQ8BAf8EBAMCBkAwgZ4GA1UdIwSBljCBk4AU3TGldJXip' .
|
||||
'N4oGS3ZYmnBDMFs8gKhd6R1MHMxCzAJBgNVBAYTAlBMMSgwJgYDVQQKDB9LcmFqb3dh' .
|
||||
'IEl6YmEgUm96bGljemVuaW93YSBTLkEuMSQwIgYDVQQDDBtDT1BFIFNaQUZJUiAtIEt' .
|
||||
'3YWxpZmlrb3dhbnkxFDASBgNVBAUTC05yIHdwaXN1OiA2ggJb9jBIBgNVHR8EQTA/MD' .
|
||||
'2gO6A5hjdodHRwOi8vd3d3Lmtpci5jb20ucGwvY2VydHlmaWthY2phX2tsdWN6eS9DU' .
|
||||
'kxfT1pLMzIuY3JsMA0GCSqGSIb3DQEBBQUAA4IBAQBYPIqnAreyeql7/opJjcar/qWZ' .
|
||||
'y9ruhB2q0lZFsJOhwgMnbQXzp/4vv93YJqcHGAXdHP6EO8FQX47mjo2ZKQmi+cIHJHL' .
|
||||
'ONdX/3Im+M17V0iNAh7Z1lOSfTRT+iiwe/F8phcEaD5q2RmvYusR7zXZq/cLL0If0hX' .
|
||||
'oPZ/EHQxjN8pxzxiUx6bJAgturnIMEfRNesxwghdr1dkUjOhGLf3kHVzgM6j3VAM7oF' .
|
||||
'mMUb5y5s96Bzl10DodWitjOEH0vvnIcsppSxH1C1dCAi0o9f/1y2XuLNhBNHMAyTqpY' .
|
||||
'PX8Yvav1c+Z50OMaSXHAnTa20zv8UtiHbaAhwlifCelUMj93S';
|
||||
|
||||
try {
|
||||
Base64::decode($str, true);
|
||||
$this->fail('Strict padding not enforced');
|
||||
} catch (\Exception $ex) {
|
||||
|
||||
$this->assertSame(
|
||||
Base64::decode($str),
|
||||
\base64_decode($str)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
|
||||
use ParagonIE\ConstantTime\Base64UrlSafe;
|
||||
use ParagonIE\ConstantTime\Binary;
|
||||
|
||||
/**
|
||||
* Class Base64UrlSafeTest
|
||||
*/
|
||||
class Base64UrlSafeTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Base64UrlSafe::encode()
|
||||
* @covers Base64UrlSafe::decode()
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws TypeError
|
||||
*/
|
||||
public function testRandom()
|
||||
{
|
||||
for ($i = 1; $i < 32; ++$i) {
|
||||
for ($j = 0; $j < 50; ++$j) {
|
||||
$random = \random_bytes($i);
|
||||
|
||||
$enc = Base64UrlSafe::encode($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64UrlSafe::decode($enc)
|
||||
);
|
||||
$this->assertSame(
|
||||
\strtr(\base64_encode($random), '+/', '-_'),
|
||||
$enc
|
||||
);
|
||||
|
||||
$unpadded = \rtrim($enc, '=');
|
||||
$this->assertSame(
|
||||
$unpadded,
|
||||
Base64UrlSafe::encodeUnpadded($random)
|
||||
);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64UrlSafe::decode($unpadded)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$random = \random_bytes(1 << 20);
|
||||
$enc = Base64UrlSafe::encode($random);
|
||||
$this->assertTrue(Binary::safeStrlen($enc) > 65536);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Base64UrlSafe::decode($enc)
|
||||
);
|
||||
$this->assertSame(
|
||||
\strtr(\base64_encode($random), '+/', '-_'),
|
||||
$enc
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,307 +0,0 @@
|
||||
<?php
|
||||
use \ParagonIE\ConstantTime\Base32;
|
||||
use \ParagonIE\ConstantTime\Base32Hex;
|
||||
use \ParagonIE\ConstantTime\Base64;
|
||||
use \ParagonIE\ConstantTime\Base64DotSlash;
|
||||
use \ParagonIE\ConstantTime\Base64DotSlashOrdered;
|
||||
use \ParagonIE\ConstantTime\Base64UrlSafe;
|
||||
use \ParagonIE\ConstantTime\Encoding;
|
||||
use \ParagonIE\ConstantTime\Hex;
|
||||
|
||||
class EncodingTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testBase32Encode()
|
||||
{
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\x00"),
|
||||
'aa======'
|
||||
);
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\x00\x00"),
|
||||
'aaaa===='
|
||||
);
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\x00\x00\x00"),
|
||||
'aaaaa==='
|
||||
);
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\x00\x00\x00\x00"),
|
||||
'aaaaaaa='
|
||||
);
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\x00\x00\x00\x00\x00"),
|
||||
'aaaaaaaa'
|
||||
);
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\x00\x00\x0F\xFF\xFF"),
|
||||
'aaaa7777'
|
||||
);
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\xFF\xFF\xF0\x00\x00"),
|
||||
'7777aaaa'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\xce\x73\x9c\xe7\x39"),
|
||||
'zzzzzzzz'
|
||||
);
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode("\xd6\xb5\xad\x6b\x5a"),
|
||||
'22222222'
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\x00"),
|
||||
'AA======'
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\x00\x00"),
|
||||
'AAAA===='
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\x00\x00\x00"),
|
||||
'AAAAA==='
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\x00\x00\x00\x00"),
|
||||
'AAAAAAA='
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\x00\x00\x00\x00\x00"),
|
||||
'AAAAAAAA'
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\x00\x00\x0F\xFF\xFF"),
|
||||
'AAAA7777'
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\xFF\xFF\xF0\x00\x00"),
|
||||
'7777AAAA'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\xce\x73\x9c\xe7\x39"),
|
||||
'ZZZZZZZZ'
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32::encodeUpper("\xd6\xb5\xad\x6b\x5a"),
|
||||
'22222222'
|
||||
);
|
||||
}
|
||||
|
||||
public function testBase32Hex()
|
||||
{
|
||||
$this->assertSame(
|
||||
Base32Hex::encode("\x00"),
|
||||
'00======'
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32Hex::encode("\x00\x00"),
|
||||
'0000===='
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32Hex::encode("\x00\x00\x00"),
|
||||
'00000==='
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32Hex::encode("\x00\x00\x00\x00"),
|
||||
'0000000='
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32Hex::encode("\x00\x00\x00\x00\x00"),
|
||||
'00000000'
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32Hex::encode("\x00\x00\x0F\xFF\xFF"),
|
||||
'0000vvvv'
|
||||
);
|
||||
$this->assertSame(
|
||||
Base32Hex::encode("\xFF\xFF\xF0\x00\x00"),
|
||||
'vvvv0000'
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Based on test vectors from RFC 4648
|
||||
*/
|
||||
public function testBase32Decode()
|
||||
{
|
||||
$this->assertSame(
|
||||
"\x00\x00\x00\x00\x00\x00",
|
||||
Encoding::base32Decode('aaaaaaaaaa======')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00\x00\x00\x00\x00\x00",
|
||||
Encoding::base32Decode('aaaaaaaaaaaa====')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
Encoding::base32Decode('aaaaaaaaaaaaa===')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
Encoding::base32Decode('aaaaaaaaaaaaaaa=')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
|
||||
Encoding::base32Decode('aaaaaaaaaaaaaaaa')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00",
|
||||
Encoding::base32Decode('aa======')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00",
|
||||
Encoding::base32Decode('aaaa====')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00\x00",
|
||||
Encoding::base32Decode('aaaaa===')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00\x00\x00",
|
||||
Encoding::base32Decode('aaaaaaa=')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00\x00\x00\x00",
|
||||
Encoding::base32Decode('aaaaaaaa')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\x00\x00\x0F\xFF\xFF",
|
||||
Encoding::base32Decode('aaaa7777')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\xFF\xFF\xF0\x00\x00",
|
||||
Encoding::base32Decode('7777aaaa')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\xce\x73\x9c\xe7\x39",
|
||||
Encoding::base32Decode('zzzzzzzz')
|
||||
);
|
||||
$this->assertSame(
|
||||
"\xd6\xb5\xad\x6b\x5a",
|
||||
Encoding::base32Decode('22222222')
|
||||
);
|
||||
$this->assertSame(
|
||||
'foobar',
|
||||
Encoding::base32Decode('mzxw6ytboi======')
|
||||
);
|
||||
|
||||
$rand = random_bytes(9);
|
||||
$enc = Encoding::base32Encode($rand);
|
||||
|
||||
$this->assertSame(
|
||||
Encoding::base32Encode($rand),
|
||||
Encoding::base32Encode(Encoding::base32Decode($enc))
|
||||
);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Encoding::base32Decode($enc)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Encoding::hexDecode()
|
||||
* @covers Encoding::hexEncode()
|
||||
* @covers Encoding::base32Decode()
|
||||
* @covers Encoding::base32Encode()
|
||||
* @covers Encoding::base64Decode()
|
||||
* @covers Encoding::base64Encode()
|
||||
* @covers Encoding::base64DotSlashDecode()
|
||||
* @covers Encoding::base64DotSlashEncode()
|
||||
* @covers Encoding::base64DotSlashOrderedDecode()
|
||||
* @covers Encoding::base64DotSlashOrderedEncode()
|
||||
*/
|
||||
public function testBasicEncoding()
|
||||
{
|
||||
// Re-run the test at least 3 times for each length
|
||||
for ($j = 0; $j < 3; ++$j) {
|
||||
for ($i = 1; $i < 84; ++$i) {
|
||||
$rand = random_bytes($i);
|
||||
$enc = Encoding::hexEncode($rand);
|
||||
$this->assertSame(
|
||||
\bin2hex($rand),
|
||||
$enc,
|
||||
"Hex Encoding - Length: " . $i
|
||||
);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Encoding::hexDecode($enc),
|
||||
"Hex Encoding - Length: " . $i
|
||||
);
|
||||
|
||||
// Uppercase variant:
|
||||
$enc = Hex::encodeUpper($rand);
|
||||
$this->assertSame(
|
||||
\strtoupper(\bin2hex($rand)),
|
||||
$enc,
|
||||
"Hex Encoding - Length: " . $i
|
||||
);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Hex::decode($enc),
|
||||
"HexUpper Encoding - Length: " . $i
|
||||
);
|
||||
|
||||
$enc = Encoding::base32Encode($rand);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Encoding::base32Decode($enc),
|
||||
"Base32 Encoding - Length: " . $i
|
||||
);
|
||||
|
||||
$enc = Encoding::base32EncodeUpper($rand);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Encoding::base32DecodeUpper($enc),
|
||||
"Base32Upper Encoding - Length: " . $i
|
||||
);
|
||||
|
||||
$enc = Encoding::base32HexEncode($rand);
|
||||
$this->assertSame(
|
||||
bin2hex($rand),
|
||||
bin2hex(Encoding::base32HexDecode($enc)),
|
||||
"Base32Hex Encoding - Length: " . $i
|
||||
);
|
||||
|
||||
$enc = Encoding::base32HexEncodeUpper($rand);
|
||||
$this->assertSame(
|
||||
bin2hex($rand),
|
||||
bin2hex(Encoding::base32HexDecodeUpper($enc)),
|
||||
"Base32HexUpper Encoding - Length: " . $i
|
||||
);
|
||||
|
||||
$enc = Encoding::base64Encode($rand);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Encoding::base64Decode($enc),
|
||||
"Base64 Encoding - Length: " . $i
|
||||
);
|
||||
|
||||
$enc = Encoding::base64EncodeDotSlash($rand);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Encoding::base64DecodeDotSlash($enc),
|
||||
"Base64 DotSlash Encoding - Length: " . $i
|
||||
);
|
||||
$enc = Encoding::base64EncodeDotSlashOrdered($rand);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Encoding::base64DecodeDotSlashOrdered($enc),
|
||||
"Base64 Ordered DotSlash Encoding - Length: " . $i
|
||||
);
|
||||
|
||||
$enc = Base64UrlSafe::encode($rand);
|
||||
$this->assertSame(
|
||||
\strtr(\base64_encode($rand), '+/', '-_'),
|
||||
$enc
|
||||
);
|
||||
$this->assertSame(
|
||||
$rand,
|
||||
Base64UrlSafe::decode($enc)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
use \ParagonIE\ConstantTime\Hex;
|
||||
|
||||
class HexTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Hex::encode()
|
||||
* @covers Hex::decode()
|
||||
* @covers Hex::encodeUpper()
|
||||
*/
|
||||
public function testRandom()
|
||||
{
|
||||
for ($i = 1; $i < 32; ++$i) {
|
||||
for ($j = 0; $j < 50; ++$j) {
|
||||
$random = \random_bytes($i);
|
||||
|
||||
$enc = Hex::encode($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Hex::decode($enc)
|
||||
);
|
||||
$this->assertSame(
|
||||
\bin2hex($random),
|
||||
$enc
|
||||
);
|
||||
|
||||
$enc = Hex::encodeUpper($random);
|
||||
$this->assertSame(
|
||||
$random,
|
||||
Hex::decode($enc)
|
||||
);
|
||||
$this->assertSame(
|
||||
\strtoupper(\bin2hex($random)),
|
||||
$enc
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
<?php
|
||||
use \ParagonIE\ConstantTime\Base32;
|
||||
use \ParagonIE\ConstantTime\Base32Hex;
|
||||
use \ParagonIE\ConstantTime\Base64;
|
||||
use \ParagonIE\ConstantTime\Base64DotSlash;
|
||||
use \ParagonIE\ConstantTime\Base64DotSlashOrdered;
|
||||
use \ParagonIE\ConstantTime\Encoding;
|
||||
use \ParagonIE\ConstantTime\Hex;
|
||||
|
||||
/**
|
||||
* Class RFC4648Test
|
||||
*
|
||||
* @ref https://tools.ietf.org/html/rfc4648#section-10
|
||||
*/
|
||||
class RFC4648Test extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testVectorBase64()
|
||||
{
|
||||
$this->assertSame(Base64::encode(''), '');
|
||||
$this->assertSame(Base64::encode('f'), 'Zg==');
|
||||
$this->assertSame(Base64::encode('fo'), 'Zm8=');
|
||||
$this->assertSame(Base64::encode('foo'), 'Zm9v');
|
||||
$this->assertSame(Base64::encode('foob'), 'Zm9vYg==');
|
||||
$this->assertSame(Base64::encode('fooba'), 'Zm9vYmE=');
|
||||
$this->assertSame(Base64::encode('foobar'), 'Zm9vYmFy');
|
||||
}
|
||||
|
||||
public function testVectorBase32()
|
||||
{
|
||||
$this->assertSame(Base32::encode(''), '');
|
||||
$this->assertSame(Base32::encode('f'), 'my======');
|
||||
$this->assertSame(Base32::encode('fo'), 'mzxq====');
|
||||
$this->assertSame(Base32::encode('foo'), 'mzxw6===');
|
||||
$this->assertSame(Base32::encode('foob'), 'mzxw6yq=');
|
||||
$this->assertSame(Base32::encode('fooba'), 'mzxw6ytb');
|
||||
$this->assertSame(Base32::encode('foobar'), 'mzxw6ytboi======');
|
||||
|
||||
$this->assertSame(Base32::encodeUpper(''), '');
|
||||
$this->assertSame(Base32::encodeUpper('f'), 'MY======');
|
||||
$this->assertSame(Base32::encodeUpper('fo'), 'MZXQ====');
|
||||
$this->assertSame(Base32::encodeUpper('foo'), 'MZXW6===');
|
||||
$this->assertSame(Base32::encodeUpper('foob'), 'MZXW6YQ=');
|
||||
$this->assertSame(Base32::encodeUpper('fooba'), 'MZXW6YTB');
|
||||
$this->assertSame(Base32::encodeUpper('foobar'), 'MZXW6YTBOI======');
|
||||
}
|
||||
|
||||
public function testVectorBase32Hex()
|
||||
{
|
||||
$this->assertSame(Base32Hex::encode(''), '');
|
||||
$this->assertSame(Base32Hex::encode('f'), 'co======');
|
||||
$this->assertSame(Base32Hex::encode('fo'), 'cpng====');
|
||||
$this->assertSame(Base32Hex::encode('foo'), 'cpnmu===');
|
||||
$this->assertSame(Base32Hex::encode('foob'), 'cpnmuog=');
|
||||
$this->assertSame(Base32Hex::encode('fooba'), 'cpnmuoj1');
|
||||
$this->assertSame(Base32Hex::encode('foobar'), 'cpnmuoj1e8======');
|
||||
|
||||
$this->assertSame(Base32Hex::encodeUpper(''), '');
|
||||
$this->assertSame(Base32Hex::encodeUpper('f'), 'CO======');
|
||||
$this->assertSame(Base32Hex::encodeUpper('fo'), 'CPNG====');
|
||||
$this->assertSame(Base32Hex::encodeUpper('foo'), 'CPNMU===');
|
||||
$this->assertSame(Base32Hex::encodeUpper('foob'), 'CPNMUOG=');
|
||||
$this->assertSame(Base32Hex::encodeUpper('fooba'), 'CPNMUOJ1');
|
||||
$this->assertSame(Base32Hex::encodeUpper('foobar'), 'CPNMUOJ1E8======');
|
||||
}
|
||||
|
||||
public function testVectorBase16()
|
||||
{
|
||||
$this->assertSame(Hex::encode(''), '');
|
||||
$this->assertSame(Hex::encode('f'), '66');
|
||||
$this->assertSame(Hex::encode('fo'), '666f');
|
||||
$this->assertSame(Hex::encode('foo'), '666f6f');
|
||||
$this->assertSame(Hex::encode('foob'), '666f6f62');
|
||||
$this->assertSame(Hex::encode('fooba'), '666f6f6261');
|
||||
$this->assertSame(Hex::encode('foobar'), '666f6f626172');
|
||||
|
||||
$this->assertSame(Hex::encodeUpper(''), '');
|
||||
$this->assertSame(Hex::encodeUpper('f'), '66');
|
||||
$this->assertSame(Hex::encodeUpper('fo'), '666F');
|
||||
$this->assertSame(Hex::encodeUpper('foo'), '666F6F');
|
||||
$this->assertSame(Hex::encodeUpper('foob'), '666F6F62');
|
||||
$this->assertSame(Hex::encodeUpper('fooba'), '666F6F6261');
|
||||
$this->assertSame(Hex::encodeUpper('foobar'), '666F6F626172');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user