Laravel的应用程序密钥 - 它是什么以及它是如何工作的?

时间:2016-08-16 17:17:35

标签: php laravel frameworks

据我所知,Laravel中的app键为会话和敏感数据提供保护,但我想了解的是它究竟是如何工作的?它的用途是什么? 我找不到任何有关它的信息。 提前谢谢!

6 个答案:

答案 0 :(得分:8)

评论here表示它已在ecrypter中使用。我发现它herehereopenssl_encryptopenssl_decrypt一起使用。没有该密钥,您无法解密使用这两个功能加密的任何内容,例如存储在用户计算机上的会话cookie。如果他们没有加密,任何有权访问它们的人都可以像你一样登录该应用程序。

答案 1 :(得分:7)

使用位置:

应用程序中每个使用加密(而不是哈希)的Laravel组件都使用APP_KEY。 (会话,CSRF令牌和Cookies)

不使用的地方:

使用哈希进行搜索的地方,例如密码,password_reset_token

因此,更改APP_KEY不会对您的密码或password_reset令牌造成任何问题。

工作方式:

APP_KEY是您的应用程序中一个没人知道的私有字符串(encryption_key)。因此,如果只有您的应用程序知道该密钥,则只有您的应用程序才能解密由该密钥加密的数据。这就是其安全性的工作方式。

**有关其工作原理的更多信息,您只需在项目中检查以下文件即可:EncryptionServiceProvider.php

一些最佳做法是:

  • 仅将其存储在.env文件中。 (请勿将其存储在config / app.php或任何GIT跟踪的文件中)
  • 仅在出现以下情况时更改它:
    • 您发现钥匙可能已泄漏。 (以便其他人可以解密您的数据)
    • 您要注销所有用户(由会话管理的用户而不是api令牌)
    • 您要使cookie无效。

答案 2 :(得分:2)

APP_KEY用于加密,而不用于散列。您在应用程序中加密的每个数据都在后台使用APP_KEY。请记住,加密数据可以解密,但散列数据不能解密。

一个常见的对APP_KEY的误解是,它与密码哈希相关,事实并非如此。这是证明。

taylor's tweet

您可以在上面的推文中看到APP_KEY与 HASHED 数据无关

答案 3 :(得分:1)

实际上,应用程序密钥用于laravel中的所有加密数据。如果未在.env中配置应用程序密钥,则您的所有会话和其他加密数据将不安全!

更多laravel docs搜索应用程序密钥

答案 4 :(得分:1)

App Key用于所有加密数据,如会话,密码,记忆令牌等。 创建app key:generate。

后,使用Hash :: make()保存的密码将不再有效

你可以从link1link2

那里得到一些想法

答案 5 :(得分:0)

如果您查看laravel核心,则有一个使用app_key的Encryptor类(名称空间Illuminate \ Encryption)。

是一种方法
/**
 * Encrypt the given value.
 *
 * @param  mixed  $value
 * @param  bool  $serialize
 * @return string
 *
 * @throws \Illuminate\Contracts\Encryption\EncryptException
 */
public function encrypt($value, $serialize = true)
{
    $iv = random_bytes(openssl_cipher_iv_length($this->cipher));

    // First we will encrypt the value using OpenSSL. After this is encrypted we
    // will proceed to calculating a MAC for the encrypted value so that this
    // value can be verified later as not having been changed by the users.
    $value = \openssl_encrypt(
        $serialize ? serialize($value) : $value,
        $this->cipher, $this->key, 0, $iv
    );

    if ($value === false) {
        throw new EncryptException('Could not encrypt the data.');
    }

    // Once we get the encrypted value we'll go ahead and base64_encode the input
    // vector and create the MAC for the encrypted value so we can then verify
    // its authenticity. Then, we'll JSON the data into the "payload" array.
    $mac = $this->hash($iv = base64_encode($iv), $value);

    $json = json_encode(compact('iv', 'value', 'mac'));

    if (json_last_error() !== JSON_ERROR_NONE) {
        throw new EncryptException('Could not encrypt the data.');
    }

    return base64_encode($json);
}

此方法在2个地方用于会话和cookie。这是方法

这是用于会话

/**
 * Prepare the serialized session data for storage.
 *
 * @param  string  $data
 * @return string
 */
protected function prepareForStorage($data)
{
    return $this->encrypter->encrypt($data);
}

这是给Cookies的

/**
 * Encrypt the cookies on an outgoing response.
 *
 * @param  \Symfony\Component\HttpFoundation\Response  $response
 * @return \Symfony\Component\HttpFoundation\Response
 */
protected function encrypt(Response $response)
{
    foreach ($response->headers->getCookies() as $cookie) {
        if ($this->isDisabled($cookie->getName())) {
            continue;
        }

        $response->headers->setCookie($this->duplicate(
            $cookie, $this->encrypter->encrypt($cookie->getValue(), static::serialized($cookie->getName()))
        ));
    }

    return $response;
}

当然,还有其他使用其自己的Crypto方法的软件包,例如供应商文件夹中的Swift Mailer。

相关问题