/** * Crea una clave temporal para encriptar la clave maestra y guardarla. * * @param int $maxTime El tiempo máximo de validez de la clave * @return bool|string */ public static function setTempMasterPass($maxTime = 14400) { // Encriptar la clave maestra con hash aleatorio generado $randomKey = Crypt::generateAesKey(Util::generate_random_bytes()); $pass = Crypt::mkCustomMPassEncrypt($randomKey, SessionUtil::getSessionMPass()); if (!is_array($pass)) { return false; } ConfigDB::setCacheConfigValue('tempmaster_pass', bin2hex($pass[0])); ConfigDB::setCacheConfigValue('tempmaster_passiv', bin2hex($pass[1])); ConfigDB::setCacheConfigValue('tempmaster_passhash', Crypt::mkHashPassword($randomKey)); ConfigDB::setCacheConfigValue('tempmaster_passtime', time()); ConfigDB::setCacheConfigValue('tempmaster_maxtime', time() + $maxTime); ConfigDB::setCacheConfigValue('tempmaster_attempts', 0); if (!ConfigDB::writeConfig(true)) { return false; } // Guardar la clave temporal hasta que finalice la sesión Session::setTemporaryMasterPass($randomKey); return $randomKey; }
/** * Desencriptar datos con la clave maestra. * * @param string $cryptData Los datos a desencriptar * @param string $cryptIV con el IV * @param string $password La clave maestra * @return string con los datos desencriptados */ public static function getDecrypt($cryptData, $cryptIV, $password = null) { if (is_null($password)) { $password = SessionUtil::getSessionMPass(); // self::getSessionMasterPass(); } $mcryptRes = self::getMcryptResource(); mcrypt_generic_init($mcryptRes, $password, $cryptIV); $strDecrypted = trim(mdecrypt_generic($mcryptRes, $cryptData)); mcrypt_generic_deinit($mcryptRes); mcrypt_module_close($mcryptRes); return $strDecrypted; }