【发布时间】:2016-12-23 04:19:59
【问题描述】:
据我所知,Laravel 中的应用密钥为会话和敏感数据提供了保护,但我想了解的是它究竟是如何工作的?它的目的是什么? 我找不到任何有关它的信息。
【问题讨论】:
-
This 是应用程序密钥的用例之一
标签: php laravel frameworks
据我所知,Laravel 中的应用密钥为会话和敏感数据提供了保护,但我想了解的是它究竟是如何工作的?它的目的是什么? 我找不到任何有关它的信息。
【问题讨论】:
标签: php laravel frameworks
在你的应用程序中使用 encyption (not hashing) 的每个 laravel 组件都使用 APP_KEY。 (会话、CSRF 令牌和 Cookie)。
larvel 使用 散列,例如 Passwords、password_reset_token。
因此,更改 APP_KEY 不会对您的密码或密码重置令牌造成任何问题。
APP_KEY 是您的应用程序中没有人知道的私有字符串 (encryption_key)。因此,如果只有您的应用程序知道密钥,那么只有您的应用程序可以解密由该密钥加密的数据。这就是它的安全工作方式。
** 有关其功能工作原理的更多信息,您只需在项目中查看此文件:EncryptionServiceProvider.php
【讨论】:
如果你查看 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。
【讨论】:
APP_KEY 用于加密而不是散列。您在应用程序中加密的每个数据都在幕后使用 APP_KEY。请记住,加密数据可以解密,但散列数据无法解密。
APP_KEY 的一个常见误解是它与密码哈希有关,但事实并非如此。这是证据。
在上面的推文中可以看到APP_KEY与HASHED数据无关
【讨论】:
【讨论】:
评论here 说它在加密器中使用。我发现 here 和 here 与 openssl_encrypt 和 openssl_decrypt 一起使用。如果没有该密钥,您将无法解密使用这两个功能加密的任何内容,例如存储在用户计算机上的会话 cookie。如果他们没有加密任何有权访问他们的人都可以以您的身份登录应用程序。
【讨论】:
其实应用密钥是用于laravel中所有的加密数据。如果.env中没有配置应用密钥,你的所有会话和其他加密数据都将不安全!
更多laravel docs搜索应用密钥
【讨论】: