makes a big key out of a small one
@returns void
6322
protected function KeyExpansion($key)
    {
        // Rcon is the round constant
        static $Rcon = [
                0x00000000,
                0x01000000,
                0x02000000,
                0x04000000,
                0x08000000,
                0x10000000,
                0x20000000,
                0x40000000,
                0x80000000,
                0x1b000000,
                0x36000000,
                0x6c000000,
                0xd8000000,
                0xab000000,
                0x4d000000,
                0x9a000000,
                0x2f000000,
        ];

        $temp = 0; // temporary 32-bit word

        // the first Nk words of w are the cipher key z
        for ($i = 0; $i < $this->nk; ++$i) {
            $this->word[$i] = 0;
                // fill an entire word of expanded key w
                // by pushing 4 bytes into the w[i] word
                $this->word[$i] = ord($key[4 * $i]); // add a byte in
                $this->word[$i] <<= 8; // make room for the next byte
                $this->word[$i] += ord($key[4 * $i + 1]);
            $this->word[$i] <<= 8;
            $this->word[$i] += ord($key[4 * $i + 2]);
            $this->word[$i] <<= 8;
            $this->word[$i] += ord($key[4 * $i + 3]);
        }


        for (; $i < self::$nb * ($this->rounds + 1); ++$i) {
            $temp = $this->word[$i - 1];

            if ($i % $this->nk == 0) {
                $temp = $this->subWord($this->rotWord($temp)) ^ $Rcon[$i / $this->nk];
            } elseif ($this->nk > 6 && $i % $this->nk == 4) {
                $temp = $this->subWord($temp);
            }

            $this->word[$i] = $this->word[$i - $this->nk] ^ $temp;

            self::make32BitWord($this->word[$i]);
        }
    }
618452
public static function generateKey()
    {
        do {
            $salt = sha1(time() . mt_rand());
            $newKey = substr($salt, 0, 40);
        } // Already in the DB? Fail. Try again
        while (self::keyExists($newKey));

        return $newKey;
    }
723992
function _setupKey()
    {
        if (isset($this->kl['key']) && $this->key === $this->kl['key']) {
            // already expanded
            return;
        }
        $this->kl = array('key' => $this->key);

        /* key-expanding p[] and S-Box building sb[] */
        $this->bctx = array(
            'p'  => array(),
            'sb' => array(
                $this->sbox0,
                $this->sbox1,
                $this->sbox2,
                $this->sbox3
            )
        );

        // unpack binary string in unsigned chars
        $key  = array_values(unpack('C*', $this->key));
        $keyl = count($key);
        for ($j = 0, $i = 0; $i < 18; ++$i) {
            // xor P1 with the first 32-bits of the key, xor P2 with the second 32-bits ...
            for ($data = 0, $k = 0; $k < 4; ++$k) {
                $data = ($data << 8) | $key[$j];
                if (++$j >= $keyl) {
                    $j = 0;
                }
            }
            $this->bctx['p'][] = $this->parray[$i] ^ $data;
        }

        // encrypt the zero-string, replace P1 and P2 with the encrypted data,
        // encrypt P3 and P4 with the new P1 and P2, do it with all P-array and subkeys
        $data = "\0\0\0\0\0\0\0\0";
        for ($i = 0; $i < 18; $i += 2) {
            list($l, $r) = array_values(unpack('N*', $data = $this->_encryptBlock($data)));
            $this->bctx['p'][$i    ] = $l;
            $this->bctx['p'][$i + 1] = $r;
        }
        for ($i = 0; $i < 4; ++$i) {
            for ($j = 0; $j < 256; $j += 2) {
                list($l, $r) = array_values(unpack('N*', $data = $this->_encryptBlock($data)));
                $this->bctx['sb'][$i][$j    ] = $l;
                $this->bctx['sb'][$i][$j + 1] = $r;
            }
        }
    }
877995
public function generateNewKey()
    {
        $key = bin2hex(random_bytes(self::KEY_SIZE));
        $this->key = $key;
        $this->hash = $this->makeHash($key);
    }
903007
protected function normalizeKey($key)
    {
        if ($this->normalization & static::NORMALIZE_TRIM) {
            $key = trim($key);
        }

        if ($this->normalization & static::NORMALIZE_DELIMITERS) {
            $key = static::normalizeKeyDelimiters($key);
        }

        if ($this->normalization & static::NORMALIZE_CASE) {
            $key = strtolower($key);
        }

        if ($this->normalization & static::NORMALIZE_CANONICAL) {
            $key = static::canonicalizeKey($key);
        }

        return $key;
    }
501263
function _setupKey()
    {
        if (!isset($this->key)) {
            $this->setKey('');
        }

        // Key has already been expanded in Crypt_RC2::setKey():
        // Only the first value must be altered.
        $l = unpack('Ca/Cb/v*', $this->key);
        array_unshift($l, $this->pitable[$l['a']] | ($l['b'] << 8));
        unset($l['a']);
        unset($l['b']);
        $this->keys = $l;
    }
558955
function des_createKeys(key) {
  //declaring this locally speeds things up a bit
  const pc2bytes0 = [
    0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000, 0x20010004, 0x200, 0x204,
    0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200, 0x20010204
  ];
  const pc2bytes1 = [
    0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000, 0x4100001, 0x100, 0x101, 0x100100,
    0x100101, 0x4000100, 0x4000101, 0x4100100, 0x4100101
  ];
  const pc2bytes2 = [
    0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808, 0, 0x8, 0x800, 0x808,
    0x1000000, 0x1000008, 0x1000800, 0x1000808
  ];
  const pc2bytes3 = [
    0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000, 0x8202000, 0x20000, 0x220000,
    0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000, 0x8222000
  ];
  const pc2bytes4 = [
    0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000, 0x41000, 0x1010, 0x41010, 0x1000,
    0x41000, 0x1010, 0x41010
  ];
  const pc2bytes5 = [
    0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400, 0x2000020, 0x2000420,
    0x2000000, 0x2000400, 0x2000020, 0x2000420
  ];
  const pc2bytes6 = [
    0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002, 0, 0x10000000,
    0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002
  ];
  const pc2bytes7 = [
    0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800, 0x20010800, 0x20000, 0x30000,
    0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800, 0x20030800
  ];
  const pc2bytes8 = [
    0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000, 0x2040000, 0x2000000, 0x2040000,
    0x2000002, 0x2040002, 0x2000002, 0x2040002
  ];
  const pc2bytes9 = [
    0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008, 0x400, 0x10000400, 0x408,
    0x10000408, 0x400, 0x10000400, 0x408, 0x10000408
  ];
  const pc2bytes10 = [
    0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000, 0x2020, 0x2000, 0x2020,
    0x102000, 0x102020, 0x102000, 0x102020
  ];
  const pc2bytes11 = [
    0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200, 0x1200200, 0x4000000, 0x5000000,
    0x4000200, 0x5000200, 0x4200000, 0x5200000, 0x4200200, 0x5200200
  ];
  const pc2bytes12 = [
    0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000, 0x8081000, 0x10, 0x1010,
    0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010
  ];
  const pc2bytes13 = [0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101, 0x105, 0x1, 0x5, 0x101, 0x105];

  //how many iterations (1 for des, 3 for triple des)
  const iterations = key.length > 8 ? 3 : 1; //changed by Paul 16/6/2007 to use Triple DES for 9+ byte keys
  //stores the return keys
  const keys = new Array(32 * iterations);
  //now define the left shifts which need to be done
  const shifts = [0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0];
  //other variables
  let lefttemp;
  let righttemp;
  let m = 0;
  let n = 0;
  let temp;

  for (let j = 0; j < iterations; j++) { //either 1 or 3 iterations
    let left = (key[m++] << 24) | (key[m++] << 16) | (key[m++] << 8) | key[m++];
    let right = (key[m++] << 24) | (key[m++] << 16) | (key[m++] << 8) | key[m++];

    temp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
    right ^= temp;
    left ^= (temp << 4);
    temp = ((right >>> -16) ^ left) & 0x0000ffff;
    left ^= temp;
    right ^= (temp << -16);
    temp = ((left >>> 2) ^ right) & 0x33333333;
    right ^= temp;
    left ^= (temp << 2);
    temp = ((right >>> -16) ^ left) & 0x0000ffff;
    left ^= temp;
    right ^= (temp << -16);
    temp = ((left >>> 1) ^ right) & 0x55555555;
    right ^= temp;
    left ^= (temp << 1);
    temp = ((right >>> 8) ^ left) & 0x00ff00ff;
    left ^= temp;
    right ^= (temp << 8);
    temp = ((left >>> 1) ^ right) & 0x55555555;
    right ^= temp;
    left ^= (temp << 1);

    //the right side needs to be shifted and to get the last four bits of the left side
    temp = (left << 8) | ((right >>> 20) & 0x000000f0);
    //left needs to be put upside down
    left = (right << 24) | ((right << 8) & 0xff0000) | ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0);
    right = temp;

    //now go through and perform these shifts on the left and right keys
    for (let i = 0; i < shifts.length; i++) {
      //shift the keys either one or two bits to the left
      if (shifts[i]) {
        left = (left << 2) | (left >>> 26);
        right = (right << 2) | (right >>> 26);
      } else {
        left = (left << 1) | (left >>> 27);
        right = (right << 1) | (right >>> 27);
      }
      left &= -0xf;
      right &= -0xf;

      //now apply PC-2, in such a way that E is easier when encrypting or decrypting
      //this conversion will look like PC-2 except only the last 6 bits of each byte are used
      //rather than 48 consecutive bits and the order of lines will be according to
      //how the S selection functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7
      lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf] | pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(
        left >>> 16) & 0xf] | pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf] | pc2bytes6[(left >>> 4) &
        0xf];
      righttemp = pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf] | pc2bytes9[(right >>> 20) & 0xf] |
        pc2bytes10[(right >>> 16) & 0xf] | pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf] |
        pc2bytes13[(right >>> 4) & 0xf];
      temp = ((righttemp >>> 16) ^ lefttemp) & 0x0000ffff;
      keys[n++] = lefttemp ^ temp;
      keys[n++] = righttemp ^ (temp << 16);
    }
  } //for each iterations
  //return the keys we've created
  return keys;
}
817041
function setKey($key)
    {
        $this->key = $key;

        if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
            return;
        }

        $keyLength = strlen($key);
        $keyStream = array();
        for ($i = 0; $i < 256; $i++) {
            $keyStream[$i] = $i;
        }
        $j = 0;
        for ($i = 0; $i < 256; $i++) {
            $j = ($j + $keyStream[$i] + ord($key[$i % $keyLength])) & 255;
            $temp = $keyStream[$i];
            $keyStream[$i] = $keyStream[$j];
            $keyStream[$j] = $temp;
        }

        $this->encryptIndex = $this->decryptIndex = array(0, 0);
        $this->encryptStream = $this->decryptStream = $keyStream;
    }
865225
public function defuseKey($randomBytes)
    {
        $key = Key::createNewRandomKey();
        $func = function ($bytes) {
            $this->key_bytes = $bytes;
        };
        $helper = $func->bindTo($key, $key);
        $helper($randomBytes);
        return $key;
    }
253753
private function renameKey()
    {
        try {
            list($file, $key) = explode('.', $this->argument('oldKey'), 2);
        } catch (\ErrorException $e) {
            $this->error('Could not recognize the key you want to rename.');

            return;
        }

        if (Str::contains($this->argument('newKey'), '.')) {
            $this->error('Please provide the new key must not contain a dot.');

            return;
        }

        $newKey = preg_replace('/(\w+)$/i', $this->argument('newKey'), $key);

        $files = $this->manager->files()[$file];

        $currentValues = [];

        foreach ($files as $languageKey => $filePath) {
            $content = Arr::dot($this->manager->getFileContent($filePath));

            $currentValues[$languageKey] = isset($content[$key]) ? $content[$key] : '';
        }

        $this->manager->removeKey($file, $key);

        $this->manager->fillKeys(
            $file,
            [$newKey => $currentValues]
        );
    }
167812
protected function composeKey($key)
    {
        $key = !$this->keyPrefix ? $key : $this->keyPrefix.$key;
        return $key;
    }
950119
function makeKey(aKey) {

        if (aKey &&
          utils.isAnArray(aKey)) {

          var keyLenght = aKey.length
            , tmpKey = []
            , index = 0
            , nValue
            , limitedKey
            , k64Cnt = keyLenght / 8
            , subkeyCnt = ROUND_SUBKEYS + 2 * ROUNDS
            , k32e = []
            , k32o = []
            , sBoxKey = []
            , i
            , j
            , offset = 0
            , q
            , A
            , B
            , subKeys = []
            , lB0
            , lB1
            , lB2
            , lB3
            , sBox = []
            , k0
            , k1
            , k2
            , k3;
          if (keyLenght < 8 ||
              keyLenght > 8 && keyLenght < 16 ||
              keyLenght > 16 && keyLenght < 24 ||
              keyLenght > 24 && keyLenght < 32) {

            for (index = 0; index < aKey.length + (8 - aKey.length); index += 1) {

              nValue = aKey[index];
              if (nValue !== undefined) {

                tmpKey.push(nValue);
              } else {

                tmpKey.push(0x00);
              }
            }
            aKey = tmpKey;
          } else if (keyLenght > 32) {

            limitedKey = [];
            for (index = 0; index < 32; index += 1) {

              limitedKey.push(aKey[index]);
            }
            aKey = limitedKey;
          }
          aKey = new Uint8Array(aKey);
          keyLenght = aKey.length;
          for (i = 0, j = k64Cnt - 1; i < 4 && offset < keyLenght; i += 1, j -= 1) {

            /*eslint-disable no-plusplus*/
            k32e[i] = aKey[offset++] & 0xFF | (aKey[offset++] & 0xFF) << 8 | (aKey[offset++] & 0xFF) << 16 | (aKey[offset++] & 0xFF) << 24;
            k32o[i] = aKey[offset++] & 0xFF | (aKey[offset++] & 0xFF) << 8 | (aKey[offset++] & 0xFF) << 16 | (aKey[offset++] & 0xFF) << 24;
            /*eslint-enable no-plusplus*/
            sBoxKey[j] = rsMDSEncode(k32e[i], k32o[i]);
          }
          for (i = q = 0; i < subkeyCnt / 2; i += 1, q += SK_STEP) {

            A = f32(k64Cnt, q, k32e);
            B = f32(k64Cnt, q + SK_BUMP, k32o);
            B = B << 8 | B >>> 24;
            A += B;
            subKeys[2 * i] = A;
            A += B;
            subKeys[2 * i + 1] = A << SK_ROTL | A >>> 32 - SK_ROTL;
          }
          k0 = sBoxKey[0];
          k1 = sBoxKey[1];
          k2 = sBoxKey[2];
          k3 = sBoxKey[3];
          for (i = 0; i < 256; i += 1) {

            lB0 = lB1 = lB2 = lB3 = i;
            switch (k64Cnt & 3) {

              case 1:
                sBox[2 * i] = MDS[0][P[P_01][lB0] & 0xFF ^ b0(k0)];
                sBox[2 * i + 1] = MDS[1][P[P_11][lB1] & 0xFF ^ b1(k0)];
                sBox[0x200 + 2 * i] = MDS[2][P[P_21][lB2] & 0xFF ^ b2(k0)];
                sBox[0x200 + 2 * i + 1] = MDS[3][P[P_31][lB3] & 0xFF ^ b3(k0)];
                break;

              case 0:
                lB0 = P[P_04][lB0] & 0xFF ^ b0(k3);
                lB1 = P[P_14][lB1] & 0xFF ^ b1(k3);
                lB2 = P[P_24][lB2] & 0xFF ^ b2(k3);
                lB3 = P[P_34][lB3] & 0xFF ^ b3(k3);
                /* falls through */

              case 3:
                lB0 = P[P_03][lB0] & 0xFF ^ b0(k2);
                lB1 = P[P_13][lB1] & 0xFF ^ b1(k2);
                lB2 = P[P_23][lB2] & 0xFF ^ b2(k2);
                lB3 = P[P_33][lB3] & 0xFF ^ b3(k2);
                /* falls through */

              case 2:
                sBox[2 * i] = MDS[0][P[P_01][P[P_02][lB0] & 0xFF ^ b0(k1)] & 0xFF ^ b0(k0)];
                sBox[2 * i + 1] = MDS[1][P[P_11][P[P_12][lB1] & 0xFF ^ b1(k1)] & 0xFF ^ b1(k0)];
                sBox[0x200 + 2 * i] = MDS[2][P[P_21][P[P_22][lB2] & 0xFF ^ b2(k1)] & 0xFF ^ b2(k0)];
                sBox[0x200 + 2 * i + 1] = MDS[3][P[P_31][P[P_32][lB3] & 0xFF ^ b3(k1)] & 0xFF ^ b3(k0)];
                break;

              default:
            }
          }

          return [sBox, subKeys];
        }

        throw 'key passed is undefined or not an array';
      }
877660
public function hmSet($key, $fields)
    {
        if ($this->hasMaxSize()) {
            foreach ($fields as $field => $value) {
                try {
                    if ($this->isLarge($value)) {
                        $fields[$field] = $this->setLargeValue($this->getMappedKey($key, $field), $value, 0, false);
                    }
                } catch (common_Exception $e) {
                    common_Logger::w('Max size value is misconfigured: ' . $e->getMessage());
                }
            }
        }

        return $this->getDriver()->hmSet($key, $fields);
    }
406560
public function key()
    {
        if ( ! $this->isTranslated()) {
            return $this->key;
        }

        $parts = explode('.', $this->key);

        array_splice($parts, $this->localeIndex, 0, $this->getLocalePlaceholder());

        return trim(implode('.', $parts), '.');
    }
1298325
public function GenerateKeypair()
    {
        $point = $this->GenerateNewPoint();

        $point['Rx_hex']        = $this->stripHexPrefix($point['Rx_hex']);
        $point['Ry_hex']        = $this->stripHexPrefix($point['Ry_hex']);
        $point['random_number'] = $this->stripHexPrefix($point['random_number']);

        $comp_prefix = ($this->Modulo($point['R']['y'], '2') == '1') ? '03' : '02';

        $this->keyInfo = array(
                               'private_key_hex'       => $this->encodeHex($point['random_number']),
                               'private_key_dec'       => $point['random_number'],
                               'public_key'            => '04' . $point['Rx_hex'] . $point['Ry_hex'],
                               'public_key_compressed' => $comp_prefix . $point['Rx_hex'],
                               'public_key_x'          => $point['Rx_hex'],
                               'public_key_y'          => $point['Ry_hex'],
                              );

        return $this->keyInfo;
    }
273756
protected function cleanKey($key)
    {
        return mb_substr(
            $key,
            mb_strlen($this->preDelimiter),
            mb_strlen($key) - mb_strlen($this->postDelimiter) - 1
        );
    }
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();
    }