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

Posted

技术标签:

【中文标题】Laravel 应用程序密钥 - 它是啥以及它是如何工作的?【英文标题】:Laravel's application key - what it is and how does it work?Laravel 应用程序密钥 - 它是什么以及它是如何工作的? 【发布时间】:2016-12-23 04:19:59 【问题描述】:

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

【问题讨论】:

This 是应用程序密钥的用例之一 【参考方案1】:

使用地点:

在你的应用程序中使用 encyption (not hashing) 的每个 laravel 组件都使用 APP_KEY。 (会话、CSRF 令牌和 Cookie)

不使用的地方:

larvel 使用 散列,例如 Passwords、password_reset_token

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

工作原理:

APP_KEY 是您的应用程序中没有人知道的私有字符串 (encryption_key)。因此,如果只有您的应用程序知道密钥,那么只有您的应用程序可以解密由该密钥加密的数据。这就是它的安全工作方式。

** 有关其功能工作原理的更多信息,您只需在项目中查看此文件:EncryptionServiceProvider.php

一些最佳做法是:

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

【讨论】:

【参考方案2】:

如果你查看 laravel 核心,有一个使用 app_key 的加密器类(命名空间 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);

这个方法用于会话和 cookie 的两个地方。这是方法

这是为会议准备的

/**
 * Prepare the serialized session data for storage.
 *
 * @param  string  $data
 * @return string
 */
protected function prepareForStorage($data)

    return $this->encrypter->encrypt($data);

这是给 Cookie 的

/**
 * 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 方法,例如 vendor 文件夹中的 Swift Mailer。

【讨论】:

【参考方案3】:

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

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

taylor's tweet

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

【讨论】:

【参考方案4】:

App Key 用于所有加密数据,如会话、密码、记住令牌等。 使用 Hash::make() 保存的密码在 create app key:generate 后将不再有效。

您可以从link1 和link2 获得一些想法

【讨论】:

哈希不使用APP_KEY,只加密:twitter.com/taylorotwell/status/1027290106648875008 您的回答部分正确。 APP_KEY 用于加密,但“密码”没有加密,它们是不可逆的散列,因此没有使用密钥的意义。【参考方案5】:

评论here 说它在加密器中使用。我发现 here 和 here 与 openssl_encrypt 和 openssl_decrypt 一起使用。如果没有该密钥,您将无法解密使用这两个功能加密的任何内容,例如存储在用户计算机上的会话 cookie。如果他们没有加密任何有权访问他们的人都可以以您的身份登录应用程序。

【讨论】:

【参考方案6】:

其实应用密钥是用于laravel中所有的加密数据。如果.env中没有配置应用密钥,你的所有会话和其他加密数据都将不安全!

更多laravel docs搜索应用密钥

【讨论】:

我已阅读此内容。但我的问题是这个应用程序密钥实际上是做什么来保护加密数据的?

以上是关于Laravel 应用程序密钥 - 它是啥以及它是如何工作的?的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 中的控制台路由是啥?它是如何工作的? [关闭]

.NET 中的 IoC 是啥 [重复]

Hashgraph,它是啥以及它是如何工作的?

TVOS UITableViewCell _UIFloatingContentView 它是啥以及为啥?

SignInManager,它是啥以及如何使用,何时使用?

Plist:它是啥以及如何使用它