get_site_hash()); } public function __construct($salt) { $this->salt = $salt; } /** * Encodes and sign data as a string to be sent to the browser and back to the server, * ensuring the content has not been altered. This allows for the controllers to be stateless. */ public function encode(array $data) { $hash = $this->getHash($data); return base64_encode(json_encode(['data' => $data, 'hash' => $hash])); } public function decode($string) { if (! $string = base64_decode($string)) { return null; } if (! $decoded = json_decode($string, true)) { return null; } $hash = $this->getHash($decoded['data']); if ($hash === $decoded['hash']) { return $decoded['data']; } } private function getHash(array $data) { $string = json_encode($data); return sha1($string . $this->salt); } }