. */ namespace SP\Core\Crypt; use Defuse\Crypto\Exception\CryptoException; /** * Class Vault * * @package SP\Core\Crypt */ final class Vault { /** * @var string */ private $data; /** * @var string */ private $key; /** * @var int */ private $timeSet = 0; /** * @var int */ private $timeUpdated = 0; /** * @return static */ public static function getInstance(): Vault { return new Vault(); } /** * Regenerar la clave de sesión * * @param string $newSeed * @param string $oldSeed * * @return Vault * @throws CryptoException */ public function reKey(string $newSeed, string $oldSeed): Vault { $this->timeUpdated = time(); $sessionMPass = $this->getData($oldSeed); $this->saveData($sessionMPass, $newSeed); return $this; } /** * Devolver la clave maestra de la sesión * * @param string $key * * @return string * @throws CryptoException */ public function getData(string $key): string { return Crypt::decrypt($this->data, $this->key, $key); } /** * Guardar la clave maestra en la sesión * * @param mixed $data * @param string $key * * @return $this * @throws CryptoException */ public function saveData($data, string $key): Vault { if ($this->timeSet === 0) { $this->timeSet = time(); } $this->key = Crypt::makeSecuredKey($key); $this->data = Crypt::encrypt($data, $this->key, $key); return $this; } /** * @return int */ public function getTimeSet(): int { return $this->timeSet; } /** * @return int */ public function getTimeUpdated(): int { return $this->timeUpdated; } /** * Serializaes the current object */ public function getSerialized(): string { return serialize($this); } }