phpseclib <-> OpenSSL Mode Mapper

May need to be overwritten by classes extending this one in some cases

@return int
@access private
430640
function _openssl_translate_mode()
    {
        switch ($this->mode) {
            case self::MODE_ECB:
                return 'ecb';
            case self::MODE_CBC:
                return 'cbc';
            case self::MODE_CTR:
                return 'ctr';
            case self::MODE_CFB:
                return 'cfb';
            case self::MODE_CFB8:
                return 'cfb8';
            case self::MODE_OFB:
                return 'ofb';
        }
    }
1232795
function _setupKey()
    {
        switch (true) {
            // if $key <= 64bits we configure our internal pure-php cipher engine
            // to act as regular [1]DES, not as 3DES. mcrypt.so::tripledes does the same.
            case strlen($this->key) <= 8:
                $this->des_rounds = 1;
                break;

            // otherwise, if $key > 64bits, we configure our engine to work as 3DES.
            default:
                $this->des_rounds = 3;

                // (only) if 3CBC is used we have, of course, to setup the $des[0-2] keys also separately.
                if ($this->mode_3cbc) {
                    $this->des[0]->_setupKey();
                    $this->des[1]->_setupKey();
                    $this->des[2]->_setupKey();

                    // because $des[0-2] will, now, do all the work we can return here
                    // not need unnecessary stress parent::_setupKey() with our, now unused, $key.
                    return;
                }
        }
        // setup our key
        parent::_setupKey();
    }
1208402
public function getKeySize() {
		switch ($this->algorithm) {
			case self::ALGORITHM_AES_128_CBC:
			case self::ALGORITHM_AES_128_CCM:
			case self::ALGORITHM_AES_128_CFB:
			case self::ALGORITHM_AES_128_CFB1:
			case self::ALGORITHM_AES_128_CFB8:
			case self::ALGORITHM_AES_128_CTR:
			case self::ALGORITHM_AES_128_ECB:
			case self::ALGORITHM_AES_128_GCM:
			case self::ALGORITHM_AES_128_OFB:
			case self::ALGORITHM_AES_128_XTS:
			case self::ALGORITHM_CAMELLIA_128_CBC:
			case self::ALGORITHM_CAMELLIA_128_CFB:
			case self::ALGORITHM_CAMELLIA_128_CFB1:
			case self::ALGORITHM_CAMELLIA_128_CFB8:
			case self::ALGORITHM_CAMELLIA_128_ECB:
			case self::ALGORITHM_CAMELLIA_128_OFB:
			case self::ALGORITHM_ID_AES128_CCM:
			case self::ALGORITHM_ID_AES128_GCM:
			case self::ALGORITHM_ID_AES128_WRAP:
			//@see: https://wiki.openssl.org/index.php/Manual:Enc(1)
			//->Blowfish and RC5 algorithms use a 128 bit key. 
			case self::ALGORITHM_BF_CBC:
			case self::ALGORITHM_BF_CFB:
			case self::ALGORITHM_BF_ECB:
			case self::ALGORITHM_BF_OFB:
			//@see http://www.gnu.org/software/gnu-crypto/manual/api/gnu/crypto/cipher/Cast5.html
			//-> since the CAST5 key schedule assumes an input key of 128 bits
			case self::ALGORITHM_CAST5_CBC:
			case self::ALGORITHM_CAST5_CFB:
			case self::ALGORITHM_CAST5_ECB:
			case self::ALGORITHM_CAST5_OFB:
			//@see https://en.wikipedia.org/wiki/International_Data_Encryption_Algorithm
			case self::ALGORITHM_IDEA_CBC:
			case self::ALGORITHM_IDEA_CFB:
			case self::ALGORITHM_IDEA_ECB:
			case self::ALGORITHM_IDEA_OFB:
			//@see https://wiki.openssl.org/index.php/Manual:Enc(1)
			case self::ALGORITHM_RC2_CBC:
			case self::ALGORITHM_RC2_CFB:
			case self::ALGORITHM_RC2_ECB:
			case self::ALGORITHM_RC2_OFB:
			case self::ALGORITHM_RC4:
			case self::ALGORITHM_SEED_CBC:
			case self::ALGORITHM_SEED_CFB:
			case self::ALGORITHM_SEED_ECB:
			case self::ALGORITHM_SEED_OFB:
				return 16;
				
			//@asee https://www.tutorialspoint.com/cryptography/triple_des.htm
			case self::ALGORITHM_DES_EDE:
			case self::ALGORITHM_DES_EDE_CBC:
			case self::ALGORITHM_DES_EDE_CFB:
			case self::ALGORITHM_DES_EDE_OFB:
				return 14;
			
			case self::ALGORITHM_AES_192_CBC:
			case self::ALGORITHM_AES_192_CCM:
			case self::ALGORITHM_AES_192_CFB:
			case self::ALGORITHM_AES_192_CFB1:
			case self::ALGORITHM_AES_192_CFB8:
			case self::ALGORITHM_AES_192_CTR:
			case self::ALGORITHM_AES_192_ECB:
			case self::ALGORITHM_AES_192_GCM:
			case self::ALGORITHM_AES_192_OFB:
			case self::ALGORITHM_CAMELLIA_192_CBC:
			case self::ALGORITHM_CAMELLIA_192_CFB:
			case self::ALGORITHM_CAMELLIA_192_CFB1:
			case self::ALGORITHM_CAMELLIA_192_CFB8:
			case self::ALGORITHM_CAMELLIA_192_ECB:
			case self::ALGORITHM_CAMELLIA_192_OFB:
			case self::ALGORITHM_ID_AES192_CCM:
			case self::ALGORITHM_ID_AES192_GCM:
			case self::ALGORITHM_ID_AES192_WRAP:
				return 24;
			
			//@asee https://www.tutorialspoint.com/cryptography/triple_des.htm
			case self::ALGORITHM_DES_EDE3:
			case self::ALGORITHM_DES_EDE3_CBC:
			case self::ALGORITHM_DES_EDE3_CFB:
			case self::ALGORITHM_DES_EDE3_CFB1:
			case self::ALGORITHM_DES_EDE3_CFB8:
			case self::ALGORITHM_DES_EDE3_OFB:
				return 21;
				
			
			case self::ALGORITHM_AES_256_CBC:
			case self::ALGORITHM_AES_256_CFB:
			case self::ALGORITHM_AES_256_CCM:
			case self::ALGORITHM_AES_256_CFB1:
			case self::ALGORITHM_AES_256_CFB8:
			case self::ALGORITHM_AES_256_CTR:
			case self::ALGORITHM_AES_256_ECB:
			case self::ALGORITHM_AES_256_GCM:
			case self::ALGORITHM_AES_256_OFB:
			case self::ALGORITHM_AES_256_XTS:
			case self::ALGORITHM_CAMELLIA_256_CBC:
			case self::ALGORITHM_CAMELLIA_256_CFB:
			case self::ALGORITHM_CAMELLIA_256_CFB1:
			case self::ALGORITHM_CAMELLIA_256_CFB8:
			case self::ALGORITHM_CAMELLIA_256_ECB:
			case self::ALGORITHM_CAMELLIA_256_OFB:
			case self::ALGORITHM_ID_AES256_CCM:
			case self::ALGORITHM_ID_AES256_GCM:
			case self::ALGORITHM_ID_AES256_WRAP:
				return 32;
			
			
			//@see https://en.wikipedia.org/wiki/Data_Encryption_Standard
			case self::ALGORITHM_DES_CBC:
			case self::ALGORITHM_DES_CFB:
			case self::ALGORITHM_DES_CFB1:
			case self::ALGORITHM_DES_CFB8:
			case self::ALGORITHM_DES_ECB:
			case self::ALGORITHM_DES_OFB:
			//@see https://en.wikipedia.org/wiki/des-X
			case self::ALGORITHM_DESX_CBC:
				return 7;
			

			case self::ALGORITHM_RC2_40_CBC:
 			case self::ALGORITHM_RC4_40:
				return 5;
				
 			case self::ALGORITHM_RC2_64_CBC:
 				return 8;
 			
			return 128;
		}
	}
1294205
public function getnonceStr()
    {
        static $nonceStr;
        if (!$nonceStr) {
            $chars    = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            $nonceStr = '';
            for ($i = 0; $i < 16; $i++) {
                $nonceStr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            }
        }

        return $nonceStr;
    }
257586
public function getCondensed()
    {
        if ($this->version === 4) {
            return $this->value;
        }

        $addr = preg_replace('/(^|:)0+(\d)/', '\1\2', $this->getExpanded());

        if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $addr, $matches, PREG_OFFSET_CAPTURE)) {
            $max = 0;
            $pos = null;

            foreach ($matches[0] as $match) {
                if (strlen($match[0]) > $max) {
                    $max = mb_strlen($match[0]);
                    $pos = $match[1];
                }
            }

            $addr = substr_replace($addr, '::', $pos, $max);
        }

        return $addr;
    }
1184040
public function toUInt8()
    {
        $binary = $this->getBinary();
        $split = str_split($binary, 1);

        // argument *by-reference*
        array_walk($split, function(&$item, $ix) {
            $buf = new Buffer($item, 1);
            $item = (int) $buf->getInt();
        });

        return $split;
    }
1202457
public function count(): int
    {
        if ($this->count == null) {
            $this->count = 0;

            if ($handle = opendir($this->fileInfo->getAbsoluteFilePath())) {
                while (($file = readdir($handle)) !== false) {
                    if (!in_array($file, array('.', '..'))) {
                        $this->count++;
                    }
                }

                closedir($handle);
            }
        }

        return $this->count;
    }
663416
private function doCountBin()
    {
        $count   = 0;
        $bitset  = $this->bitset;
        $byteLen = \strlen($bitset);
        for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
            if ($bitset[$bytePos] === "\0") {
                // fast skip null byte
                continue;
            }

            $ord = \ord($bitset[$bytePos]);
            if ($ord & 0b00000001) ++$count;
            if ($ord & 0b00000010) ++$count;
            if ($ord & 0b00000100) ++$count;
            if ($ord & 0b00001000) ++$count;
            if ($ord & 0b00010000) ++$count;
            if ($ord & 0b00100000) ++$count;
            if ($ord & 0b01000000) ++$count;
            if ($ord & 0b10000000) ++$count;
        }
        return $count;
    }
204847
private function doCountInt()
    {
        $count  = 0;
        $bitset = $this->bitset;

        // PHP does not support right shift unsigned
        if ($bitset < 0) {
            $count  = 1;
            $bitset = $bitset & \PHP_INT_MAX;
        }

        // iterate byte by byte and count set bits
        $phpIntBitSize = \PHP_INT_SIZE * 8;
        for ($bitPos = 0; $bitPos < $phpIntBitSize; $bitPos += 8) {
            $bitChk = 0xff << $bitPos;
            $byte = $bitset & $bitChk;
            if ($byte) {
                $byte = $byte >> $bitPos;
                if ($byte & 0b00000001) ++$count;
                if ($byte & 0b00000010) ++$count;
                if ($byte & 0b00000100) ++$count;
                if ($byte & 0b00001000) ++$count;
                if ($byte & 0b00010000) ++$count;
                if ($byte & 0b00100000) ++$count;
                if ($byte & 0b01000000) ++$count;
                if ($byte & 0b10000000) ++$count;
            }

            if ($bitset <= $bitChk) {
                break;
            }
        }

        return $count;
    }
354414
public function hashCode()
    {
        $validPk = null !== $this->getId();

        $validPrimaryKeyFKs = 0;
        $primaryKeyFKs = [];

        if ($validPk) {
            return crc32(json_encode($this->getPrimaryKey(), JSON_UNESCAPED_UNICODE));
        } elseif ($validPrimaryKeyFKs) {
            return crc32(json_encode($primaryKeyFKs, JSON_UNESCAPED_UNICODE));
        }

        return spl_object_hash($this);
    }
682405
private function generatePKIKeys()
    {
        // OPENSSL config
        $config = array(
            'digest_alg' => $this->settings['private_key']['digest_method'],
            'private_key_bits' => $this->settings['private_key']['bits'],
            'private_key_type' => $this->settings['private_key']['type'],
        );

        $privateKey = null;
        $resource = openssl_pkey_new($config);

        openssl_pkey_export($resource, $privateKey);

        if(!$privateKey) {
            throw new EncryptionException('Private key could not be generated');
        }

        $publicKeyDetails = openssl_pkey_get_details($resource);
        $publicKey = $publicKeyDetails['key'];

        return array($publicKey, $privateKey);
    }
335719
public function getPort()
    {
        $port = parent::getPort();

        if (JUri::getInstance()->isSsl() || ($this->isSecure() && in_array($port, ['80', '8080']))) {
            $port = '443';
        }

        return $port;
    }
1223885
public function stackTraceHashId()
    {
        if (!isset($this->stackTraceHashId)) {
            // take the lower 16 digits of the md5
            $md5 = md5(serialize($this->stackTrace));
            $this->stackTraceHashId = substr($md5, 16);
        }
        return $this->stackTraceHashId;
    }
1227869
private function getEndianess()
    {
        if (0 === self::$endianess) {
            self::$endianess = $this->fromInt32("\x01\x00\x00\x00") == 1
                             ? self::LITTLE_ENDIAN_ORDER
                             : self::BIG_ENDIAN_ORDER;
        }
        return self::$endianess;
    }
1111966
public function readPhpInt()
    {
        list(, $res) = unpack('N', $this->_rawread(4));
        if ($this->_is64bits) {
            $sres = sprintf("%u", $res);
            return (int)$sres;
        } else {
            return $res;
        }
    }
224651
public function get_identifier() {
        static $instances = 0;
        if ($this->lockidentifier === null) {
            $this->lockidentifier = md5(
                $this->get_definition()->generate_definition_hash() .
                sesskey() .
                $instances++ .
                'cache_application'
            );
        }
        return $this->lockidentifier;
    }
368593
public function size()
    {
        $size = 0;
        if ( ! is_null($this->key_)) {
            $l = strlen($this->key_);
            $size += 1 + Protobuf::size_varint($l) + $l;
        }
        if ( ! is_null($this->value_)) {
            $l = strlen($this->value_);
            $size += 1 + Protobuf::size_varint($l) + $l;
        }

        return $size;
    }