Skip to content

Commit

Permalink
Update copyright year, make totallyTyped (Psalm).
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jan 23, 2018
1 parent 7c74c5d commit 20130f4
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 15 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Paragon Initiative Enterprises
Copyright (c) 2016 - 2018 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
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<psalm
stopOnFirstError="false"
useDocblockTypes="true"
totallyTyped="false"
totallyTyped="true"
>
<projectFiles>
<directory name="src" />
Expand Down
40 changes: 37 additions & 3 deletions src/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down Expand Up @@ -150,7 +150,7 @@ protected static function decode5BitsUpper(int $src): int
* Uses bitwise operators instead of table-lookups to turn 8-bit integers
* into 5-bit integers.
*
* @param $src
* @param int $src
* @return string
*/
protected static function encode5Bits(int $src): string
Expand All @@ -169,7 +169,7 @@ protected static function encode5Bits(int $src): string
*
* Uppercase variant.
*
* @param $src
* @param int $src
* @return string
*/
protected static function encode5BitsUpper(int $src): string
Expand Down Expand Up @@ -228,14 +228,23 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
$dest = '';
// Main loop (no padding):
for ($i = 0; $i + 8 <= $srcLen; $i += 8) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 8));
/** @var int $c0 */
$c0 = static::$method($chunk[1]);
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
/** @var int $c2 */
$c2 = static::$method($chunk[3]);
/** @var int $c3 */
$c3 = static::$method($chunk[4]);
/** @var int $c4 */
$c4 = static::$method($chunk[5]);
/** @var int $c5 */
$c5 = static::$method($chunk[6]);
/** @var int $c6 */
$c6 = static::$method($chunk[7]);
/** @var int $c7 */
$c7 = static::$method($chunk[8]);

$dest .= \pack(
Expand All @@ -250,15 +259,23 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
}
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
/** @var int $c0 */
$c0 = static::$method($chunk[1]);

if ($i + 6 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
/** @var int $c2 */
$c2 = static::$method($chunk[3]);
/** @var int $c3 */
$c3 = static::$method($chunk[4]);
/** @var int $c4 */
$c4 = static::$method($chunk[5]);
/** @var int $c5 */
$c5 = static::$method($chunk[6]);
/** @var int $c6 */
$c6 = static::$method($chunk[7]);

$dest .= \pack(
Expand All @@ -270,10 +287,15 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
);
$err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5 | $c6) >> 8;
} elseif ($i + 5 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
/** @var int $c2 */
$c2 = static::$method($chunk[3]);
/** @var int $c3 */
$c3 = static::$method($chunk[4]);
/** @var int $c4 */
$c4 = static::$method($chunk[5]);
/** @var int $c5 */
$c5 = static::$method($chunk[6]);

$dest .= \pack(
Expand All @@ -285,9 +307,13 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
);
$err |= ($c0 | $c1 | $c2 | $c3 | $c4 | $c5) >> 8;
} elseif ($i + 4 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
/** @var int $c2 */
$c2 = static::$method($chunk[3]);
/** @var int $c3 */
$c3 = static::$method($chunk[4]);
/** @var int $c4 */
$c4 = static::$method($chunk[5]);

$dest .= \pack(
Expand All @@ -298,8 +324,11 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
);
$err |= ($c0 | $c1 | $c2 | $c3 | $c4) >> 8;
} elseif ($i + 3 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
/** @var int $c2 */
$c2 = static::$method($chunk[3]);
/** @var int $c3 */
$c3 = static::$method($chunk[4]);

$dest .= \pack(
Expand All @@ -309,7 +338,9 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
);
$err |= ($c0 | $c1 | $c2 | $c3) >> 8;
} elseif ($i + 2 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);
/** @var int $c2 */
$c2 = static::$method($chunk[3]);

$dest .= \pack(
Expand All @@ -319,6 +350,7 @@ protected static function doDecode(string $src, bool $upper = false, bool $stric
);
$err |= ($c0 | $c1 | $c2) >> 8;
} elseif ($i + 1 < $srcLen) {
/** @var int $c1 */
$c1 = static::$method($chunk[2]);

$dest .= \pack(
Expand Down Expand Up @@ -363,6 +395,7 @@ protected static function doEncode(string $src, bool $upper = false, $pad = true

// Main loop (no padding):
for ($i = 0; $i + 5 <= $srcLen; $i += 5) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 5));
$b0 = $chunk[1];
$b1 = $chunk[2];
Expand All @@ -381,6 +414,7 @@ protected static function doEncode(string $src, bool $upper = false, $pad = true
}
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
$b0 = $chunk[1];
if ($i + 3 < $srcLen) {
Expand Down
2 changes: 1 addition & 1 deletion src/Base32Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down
7 changes: 6 additions & 1 deletion src/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down Expand Up @@ -40,6 +40,7 @@ abstract class Base64 implements EncoderInterface
*
* @param string $src
* @return string
* @throws \TypeError
*/
public static function encode(string $src): string
{
Expand Down Expand Up @@ -72,6 +73,7 @@ protected static function doEncode(string $src, bool $pad = true): string
$srcLen = Binary::safeStrlen($src);
// Main loop (no padding):
for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3));
$b0 = $chunk[1];
$b1 = $chunk[2];
Expand All @@ -85,6 +87,7 @@ protected static function doEncode(string $src, bool $pad = true): string
}
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
$b0 = $chunk[1];
if ($i + 1 < $srcLen) {
Expand Down Expand Up @@ -155,6 +158,7 @@ public static function decode(string $src, bool $strictPadding = false): string
$dest = '';
// Main loop (no padding):
for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, 4));
$c0 = static::decode6Bits($chunk[1]);
$c1 = static::decode6Bits($chunk[2]);
Expand All @@ -171,6 +175,7 @@ public static function decode(string $src, bool $strictPadding = false): string
}
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $chunk */
$chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
$c0 = static::decode6Bits($chunk[1]);

Expand Down
2 changes: 1 addition & 1 deletion src/Base64DotSlash.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down
2 changes: 1 addition & 1 deletion src/Base64DotSlashOrdered.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down
2 changes: 1 addition & 1 deletion src/Base64UrlSafe.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down
2 changes: 1 addition & 1 deletion src/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down
2 changes: 1 addition & 1 deletion src/EncoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down
5 changes: 4 additions & 1 deletion src/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down Expand Up @@ -132,6 +132,7 @@ public static function base32HexDecodeUpper(string $str): string
*
* @param string $str
* @return string
* @throws \TypeError
*/
public static function base64Encode(string $str): string
{
Expand All @@ -156,6 +157,7 @@ public static function base64Decode(string $str): string
* Base64 character set "./[A-Z][a-z][0-9]"
* @param string $str
* @return string
* @throws \TypeError
*/
public static function base64EncodeDotSlash(string $str): string
{
Expand Down Expand Up @@ -183,6 +185,7 @@ public static function base64DecodeDotSlash(string $str): string
* Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
* @param string $str
* @return string
* @throws \TypeError
*/
public static function base64EncodeDotSlashOrdered(string $str): string
{
Expand Down
27 changes: 26 additions & 1 deletion src/Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace ParagonIE\ConstantTime;

/**
* Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
* Copyright (c) 2016 - 2018 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
Expand Down Expand Up @@ -41,12 +41,17 @@ abstract class Hex implements EncoderInterface
*/
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 */
$c = $chunk[1] & 0xf;
/** @var int $b */
$b = $chunk[1] >> 4;

$hex .= pack(
'CC',
(87 + $b + ((($b - 10) >> 8) & ~38)),
Expand All @@ -66,12 +71,19 @@ public static function encode(string $binString): string
*/
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 */
$c = $chunk[1] & 0xf;
/** @var int $b */
$b = $chunk[1] >> 4;

$hex .= pack(
'CC',
(55 + $b + ((($b - 10) >> 8) & ~6)),
Expand All @@ -92,10 +104,15 @@ public static function encodeUpper(string $binString): string
*/
public static function decode(string $hexString, bool $strictPadding = false): string
{
/** @var int $hex_pos */
$hex_pos = 0;
/** @var string $bin */
$bin = '';
/** @var int $c_acc */
$c_acc = 0;
/** @var int $hex_len */
$hex_len = Binary::safeStrlen($hexString);
/** @var int $state */
$state = 0;
if (($hex_len & 1) !== 0) {
if ($strictPadding) {
Expand All @@ -108,19 +125,27 @@ public static function decode(string $hexString, bool $strictPadding = false): s
}
}

/** @var array<int, ing> $chunk */
$chunk = \unpack('C*', $hexString);
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(
'hexEncode() only expects hexadecimal characters'
);
}
/** @var int $c_val */
$c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
if ($state === 0) {
$c_acc = $c_val * 16;
Expand Down
Loading

0 comments on commit 20130f4

Please sign in to comment.