Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Some methods renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Morselli committed Feb 1, 2016
1 parent 216b738 commit ba0c658
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/Algorithm/ContentEncryption/AESCBCHS.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class AESCBCHS implements ContentEncryptionAlgorithmInterface
*/
public function encryptContent($data, $cek, $iv, $aad, $encoded_protected_header, &$tag)
{
$k = StringUtil::substr($cek, StringUtil::strlen($cek) / 2);
$k = StringUtil::getSubString($cek, StringUtil::getStringLength($cek) / 2);

$cyphertext = AESOpenSSL::encrypt($data, $k, $iv);

Expand All @@ -50,7 +50,7 @@ public function decryptContent($data, $cek, $iv, $aad, $encoded_protected_header
return;
}

$k = StringUtil::substr($cek, StringUtil::strlen($cek) / 2);
$k = StringUtil::getSubString($cek, StringUtil::getStringLength($cek) / 2);

return AESOpenSSL::decrypt($data, $k, $iv);
}
Expand All @@ -70,8 +70,8 @@ protected function calculateAuthenticationTag($encrypted_data, $cek, $iv, $aad,
if (null !== $aad) {
$calculated_aad .= '.'.$aad;
}
$mac_key = StringUtil::substr($cek, 0, StringUtil::strlen($cek) / 2);
$auth_data_length = StringUtil::strlen($encoded_header);
$mac_key = StringUtil::getSubString($cek, 0, StringUtil::getStringLength($cek) / 2);
$auth_data_length = StringUtil::getStringLength($encoded_header);

$secured_input = implode('', [
$calculated_aad,
Expand All @@ -81,7 +81,7 @@ protected function calculateAuthenticationTag($encrypted_data, $cek, $iv, $aad,
]);
$hash = hash_hmac($this->getHashAlgorithm(), $secured_input, $mac_key, true);

return StringUtil::substr($hash, 0, StringUtil::strlen($hash) / 2);
return StringUtil::getSubString($hash, 0, StringUtil::getStringLength($hash) / 2);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Algorithm/ContentEncryption/AESOpenSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public static function decrypt($data, $k, $iv)
*/
private static function getMode($k)
{
return 'aes-'.(8 * StringUtil::strlen($k)).'-cbc';
return 'aes-'.(8 * StringUtil::getStringLength($k)).'-cbc';
}
}
2 changes: 1 addition & 1 deletion src/Algorithm/KeyEncryption/AESKW.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function checkKey(JWKInterface $key)
if ('oct' !== $key->get('kty') || !$key->has('k')) {
throw new \InvalidArgumentException('The key is not valid');
}
if ($this->getKeySize() !== StringUtil::strlen(Base64Url::decode($key->get('k')))) {
if ($this->getKeySize() !== StringUtil::getStringLength(Base64Url::decode($key->get('k')))) {
throw new \InvalidArgumentException('The key size is not valid');
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Algorithm/Signature/ECDSA.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function sign(JWKInterface $key, $data)

$part_length = $this->getSignaturePartLength();

$R = StringUtil::str_pad($this->convertDecToHex($signature->getR()), $part_length, '0', STR_PAD_LEFT);
$S = StringUtil::str_pad($this->convertDecToHex($signature->getS()), $part_length, '0', STR_PAD_LEFT);
$R = StringUtil::addPadding($this->convertDecToHex($signature->getR()), $part_length, '0', STR_PAD_LEFT);
$S = StringUtil::addPadding($this->convertDecToHex($signature->getS()), $part_length, '0', STR_PAD_LEFT);

return $this->convertHextoBin($R.$S);
}
Expand All @@ -75,15 +75,15 @@ public function verify(JWKInterface $key, $data, $signature)

$signature = $this->convertBinToHex($signature);
$part_length = $this->getSignaturePartLength();
if (StringUtil::strlen($signature) !== 2 * $part_length) {
if (StringUtil::getStringLength($signature) !== 2 * $part_length) {
return false;
}

$p = $this->getGenerator();
$x = $this->convertBase64ToDec($key->get('x'));
$y = $this->convertBase64ToDec($key->get('y'));
$R = $this->convertHexToDec(StringUtil::substr($signature, 0, $part_length));
$S = $this->convertHexToDec(StringUtil::substr($signature, $part_length));
$R = $this->convertHexToDec(StringUtil::getSubString($signature, 0, $part_length));
$S = $this->convertHexToDec(StringUtil::getSubString($signature, $part_length));
$hash = $this->convertHexToDec(hash($this->getHashAlgorithm(), $data));

$public_key = $p->getPublicKeyFrom($x, $y);
Expand Down
4 changes: 2 additions & 2 deletions src/Algorithm/Signature/HMAC.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ protected function compareHMAC($safe, $user)
if (function_exists('hash_equals')) {
return hash_equals($safe, $user);
}
$safeLen = StringUtil::strlen($safe);
$userLen = StringUtil::strlen($user);
$safeLen = StringUtil::getStringLength($safe);
$userLen = StringUtil::getStringLength($user);

if ($userLen !== $safeLen) {
return false;
Expand Down
33 changes: 28 additions & 5 deletions src/Util/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,45 @@ public static function generateRandomBytes($size)
*
* @return int
*/
public static function strlen($string)
public static function getStringLength($string)
{
return function_exists('mb_strlen') ? mb_strlen($string, '8bit') : strlen($string);
}

public static function substr($string, $start, $length = null)
/**
* @param string $string
* @param int $start
* @param null|int $length
*
* @return string
*/
public static function getSubString($string, $start, $length = null)
{
return function_exists('mb_substr') ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length);
}

public static function str_pad($input, $pad_length, $pad_string = null, $pad_style = null)
/**
* @param string $input
* @param int $pad_length
* @param null|string $pad_string
* @param null|int $pad_style
*
* @return string
*/
public static function addPadding($input, $pad_length, $pad_string = null, $pad_style = null)
{
return function_exists('mb_strlen') ? self::mb_pad_str($input, $pad_length, $pad_string, $pad_style) : str_pad($input, $pad_length, $pad_string, $pad_style);
return function_exists('mb_strlen') ? self::addPaddingMultiBytes($input, $pad_length, $pad_string, $pad_style) : str_pad($input, $pad_length, $pad_string, $pad_style);
}

private static function mb_pad_str($input, $pad_length, $pad_string = null, $pad_style = null)
/**
* @param string $input
* @param int $pad_length
* @param null|string $pad_string
* @param null|int $pad_style
*
* @return string
*/
private static function addPaddingMultiBytes($input, $pad_length, $pad_string = null, $pad_style = null)
{
return str_pad($input, strlen($input) - mb_strlen($input, '8bit') + $pad_length, $pad_string, $pad_style);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Algorithm/AESEnginesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AESEnginesTest extends \PHPUnit_Framework_TestCase
*/
public function testAES128($K, $iv, $plaintext, $expected_cyphertext)
{
$k = StringUtil::substr($K, StringUtil::strlen($K) / 2);
$k = StringUtil::getSubString($K, StringUtil::getStringLength($K) / 2);

$openssl_cyphertext = AESOpenSSL::encrypt($plaintext, $k, $iv);

Expand Down Expand Up @@ -74,7 +74,7 @@ public function getTestVectors()
private function convertArrayToBinString(array $data)
{
foreach ($data as $key => $value) {
$data[$key] = StringUtil::str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
$data[$key] = StringUtil::addPadding(dechex($value), 2, '0', STR_PAD_LEFT);
}

return hex2bin(implode('', $data));
Expand Down

0 comments on commit ba0c658

Please sign in to comment.