Set Application secret key.

@return string
@throws  Exception
547390
public function setKey()
    {
        if(trim(env('APP_KEY')) != '') {
            return true;
        }
        $appKey='';
        for($i=0;$i<32;$i++) {
            $appKey.=chr(rand(0,255));
        }
        $appKey = 'base64:'.base64_encode($appKey);
        $file = $this->basePath('.env');
        $lines = $this->parseEnvFile($file);
        $flag = false;
        foreach($lines as $i => $line) {
            $lines[$i] = trim($line);
            $lines[$i] = trim($line, "\n");
            if(strpos($line, 'APP_KEY') !== false) {
                $data = explode('=', $line, 2);
                if(!isset($data[1]) || trim($data[1]) == '') {
                    $lines[$i] = "APP_KEY=".$appKey;
                    $flag = true;
                }
            }
        }
        if($flag == false) {
            $lines = ["APP_KEY=".$appKey] + $lines;
        }
        $data = implode("\n", $lines);
        if(file_put_contents($file, $data)) {
            putenv("APP_KEY=$appKey");
            $_ENV['APP_KEY'] = $appKey;
            $_SERVER['APP_KEY'] = $appKey;
            return true;
        }
        else {
            throw new Exception('Failed to write in .env file.');
        }
    }
550726
private function getSignature(): string
    {
        if (!empty($this->secret)) {
            return $this->encode->signature(
                $this->jsonEncode($this->getHeader()),
                $this->jsonEncode($this->getPayload()),
                $this->secret
            );
        }

        throw new ValidateException('Invalid secret.', 9);
    }
561422
private function getKeyPair()
    {
        $config = $this->app->make('config/database');

        // Seee if we already have a kypair
        $keyPair = $config->get('api.keypair');

        if (!$keyPair) {
            $rsa = $this->app->make(RSA::class);

            // Generate a new RSA key
            $keyPair = $rsa->createKey(2048);

            foreach ($keyPair as &$item) {
                $item = str_replace("\r\n", "\n", $item);
            }

            // Save the keypair
            $config->save('api.keypair', $keyPair);
        }

        return $keyPair;
    }
643397
public function getKey()
    {
        if (!$this->key) {
            $this->key = sha1($this->ip.$this->route);
        }

        return $this->key;
    }
356006
public static function generateAuthenticationKey(): AuthenticationKey
    {
        // @codeCoverageIgnoreStart
        try {
            $secretKey = \random_bytes(\SODIUM_CRYPTO_AUTH_KEYBYTES);
        } catch (\Throwable $ex) {
            throw new CannotPerformOperation($ex->getMessage());
        }
        // @codeCoverageIgnoreEnd
        return new AuthenticationKey(
            new HiddenString($secretKey)
        );
    }
227143
public function getTokenResponse()
    {
        $query = [
            'grant_type' => 'client_credential',
            'appid' => $this['appid'],
            'secret' => $this['appsecret'],
        ];

        $response = Http::request('GET', static::ACCESS_TOKEN)
            ->withQuery($query)
            ->send();

        if ($response->containsKey('errcode')) {
            throw new \Exception($response['errmsg'], $response['errcode']);
        }

        return $response;
    }
1078344
public function setupSecret()
    {
        // gets the key from config.
        $secret = $this->config->get('jwt.secret');

        // if the secret is in a base64 format, unwrap it's value
        if (Str::startsWith($secret, 'base64:')) {
            // remove the 'base64:' part and decode the value.
            $secret = base64_decode(substr($secret, 7));
        }

        // set the secret on the local scope.
        $this->secret = $secret;

        // if the secret is not valid,
        // throw an exception to avoid continuing.
        if (!$this->validSecret()) {
            throw new \Exception('Invalid Secret (not present or too short). Use php artisan jwt:generate to create a valid key.');
        }
    }
836872
public function getAuthorizationBasic()
    {
        if ( ! empty($this->key) && ! empty($this->secret)) {
            $key = rawurlencode($this->key);
            $secret = rawurlencode($this->secret);

            return 'Basic ' . base64_encode($key . ':' . $secret);
        }

        return false;
    }
560595
public Key getKey() {
		if(key != null) {
			return key;
		} else if(keySpec != null && secretKeyFactory != null) {
			try {
				return key = secretKeyFactory.generateSecret(keySpec);
			} catch (InvalidKeySpecException e) {
				throw new RuntimeException(e);
			}
		}
		throw new IllegalStateException("Cannot produce key");
	}
816278
public function turnHexKeyToBin()
    {
        if (!ctype_xdigit($this->secret)) {
            throw new InvalidSecretKeyException(sprintf('The secret key "%s" is not a hex key', $this->secret));
        }

        $this->secret = hex2bin($this->secret);
    }
1276132
public function getKey()
    {
        if (!isset($this->key)) {
            $this->key = md5(microtime().rand());
        }
        return $this->key;
    }
175073
private function getMasterKey()
    {
        if (!$this->masterKey) {
            $certificatePath = 'file://'.$this->masterKeyData['cert_file'];
            $passPhrase = $this->masterKeyData['passphrase'];

            $this->masterKey = openssl_pkey_get_private($certificatePath, $passPhrase);
            if (!$this->masterKey) {
                throw new EncryptionException('Could not open master encryption key');
            }
        }
        return $this->masterKey;
    }
502183
protected function getCacheKey()
    {
        $model      = new \ReflectionClass($this->model);

        $namespace  = Str::slug($model->getNamespaceName());
        $class      = Str::slug($model->getShortName());
        $type       = Str::slug($this->type);
        $key        = $this->model->getKey();

        if (empty($key))
            throw new Exception("[Token] Error: Empty value for " . $model->getShortName() . "'s attribute '" . $this->model->getKeyName() . "'.", 1);

        return 'malahierba-token' . '---' . $namespace . '---' . $class . '---' . $type . '---' . $key;
    }
753184
public function getCacheKey()
    {
        if (is_null($this->cacheKey)) {
            return $this->prefix.$this->appId;
        }

        return $this->cacheKey;
    }
127963
private function generateKey()
    {
        $this->info('Generating application key');
        $this->callSilent('key:generate', ['--force' => true]);
        $this->advanceBar();

        $this->clearCaches();

        return $this;
    }
554571
public function preAuthCode() {
        $key = 'WeChatThirdPreAuthCode';
        if (Factory::cache()->has($key)) {
            return Factory::cache()->get($key);
        }
        $args = $this->getPreAuthCode()->json();
        if (!is_array($args)) {
            throw new \Exception('HTTP ERROR!');
        }
        if (!array_key_exists('pre_auth_code', $args)) {
            throw new \Exception(isset($args['errmsg']) ? $args['errmsg'] : 'GET ACCESS TOKEN ERROR!');
        }
        Factory::cache()->set($key, $args['pre_auth_code'], $args['expires_in']);
        return $args['pre_auth_code'];
    }
173244
public function getSecret()
    {
        if ($this->secret === null)
        {
            $salt = $this->_createSalt(12);
            $name = session_name();

            $this->secret = sha1($salt . $name);
        }

        return $this->secret;
    }