Files
sysPass/lib/SP/Core/Crypt/Crypt.php
Rubén D 1c8fb0ea1a refactor: [WIP] Use hexagonal architecture and implement interfaces for services and repositories.
Controllers are being splited into commands to better dependency management.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
2022-06-06 08:17:34 +02:00

161 lines
4.5 KiB
PHP

<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Core\Crypt;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Exception\CryptoException;
use Defuse\Crypto\Key;
use Defuse\Crypto\KeyProtectedByPassword;
/**
* Class Crypt
*
* @package SP\Core\Crypt
*/
class Crypt
{
/**
* Encriptar datos con una clave segura
*
* @param string $data
* @param string|Key $securedKey
* @param string|null $password
*
* @return string
* @throws \Defuse\Crypto\Exception\BadFormatException
* @throws \Defuse\Crypto\Exception\CryptoException
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
*/
public static function encrypt(
string $data,
$securedKey,
?string $password = null
): string
{
try {
if ($securedKey instanceof Key) {
$key = $securedKey;
} elseif (null !== $password) {
$key = self::unlockSecuredKey($securedKey, $password, false);
} else {
$key = Key::loadFromAsciiSafeString($securedKey);
}
return Crypto::encrypt($data, $key);
} catch (CryptoException $e) {
logger($e->getMessage());
throw $e;
}
}
/**
* @return string|Key
* @throws CryptoException
*/
public static function unlockSecuredKey(
string $key,
string $password,
bool $useAscii = true
)
{
try {
if ($useAscii) {
return KeyProtectedByPassword::loadFromAsciiSafeString($key)->unlockKey($password)->saveToAsciiSafeString();
}
return KeyProtectedByPassword::loadFromAsciiSafeString($key)->unlockKey($password);
} catch (CryptoException $e) {
logger($e->getMessage());
throw $e;
}
}
/**
* Desencriptar datos con una clave segura
*
* @param string $data
* @param string|Key|KeyProtectedByPassword $securedKey
* @param string|null $password
*
* @return string
* @throws \Defuse\Crypto\Exception\BadFormatException
* @throws \Defuse\Crypto\Exception\CryptoException
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
* @throws \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException
*/
public static function decrypt(
string $data,
$securedKey,
?string $password = null
): string
{
try {
if ($securedKey instanceof Key) {
return Crypto::decrypt($data, $securedKey);
}
if (null !== $password) {
if ($securedKey instanceof KeyProtectedByPassword) {
return Crypto::decrypt($data, $securedKey->unlockKey($password));
}
return Crypto::decrypt($data, self::unlockSecuredKey($securedKey, $password, false));
}
return Crypto::decrypt($data, Key::loadFromAsciiSafeString($securedKey));
} catch (CryptoException $e) {
logger($e->getMessage());
throw $e;
}
}
/**
* Securiza una clave de seguridad
*
* @return string|KeyProtectedByPassword
* @throws CryptoException
*/
public static function makeSecuredKey(
string $password,
bool $useAscii = true
)
{
try {
if ($useAscii) {
return KeyProtectedByPassword::createRandomPasswordProtectedKey($password)->saveToAsciiSafeString();
}
return KeyProtectedByPassword::createRandomPasswordProtectedKey($password);
} catch (CryptoException $e) {
logger($e->getMessage());
throw $e;
}
}
}