Add password verification and update enhancements

This commit is contained in:
netkas 2025-01-06 01:37:51 -05:00
parent 5196ac2486
commit 3e3bcfd143
3 changed files with 82 additions and 7 deletions

View file

@ -59,7 +59,10 @@
}
// Throws an exception if the hash is invalid
Cryptography::validatePasswordHash($hash);
if(!Cryptography::validatePasswordHash($hash))
{
throw new CryptographyException('Invalid password hash');
}
$encryptionKey = Configuration::getCryptographyConfiguration()->getRandomInternalEncryptionKey();
$securedPassword = Cryptography::encryptMessage($hash, $encryptionKey, Configuration::getCryptographyConfiguration()->getEncryptionKeysAlgorithm());
@ -94,7 +97,10 @@
$peerUuid = $peerUuid->getUuid();
}
Cryptography::validatePasswordHash($hash);
if(!Cryptography::validatePasswordHash($hash))
{
throw new CryptographyException('Invalid password hash');
}
$encryptionKey = Configuration::getCryptographyConfiguration()->getRandomInternalEncryptionKey();
$securedPassword = Cryptography::encryptMessage($hash, $encryptionKey, Configuration::getCryptographyConfiguration()->getEncryptionKeysAlgorithm());
@ -145,20 +151,18 @@
* Verifies a given password against a stored password hash for a specific peer.
*
* @param string|RegisteredPeerRecord $peerUuid The unique identifier of the peer, or an instance of RegisteredPeerRecord.
* @param string $hash The password hash to verify.
* @param string $sha512 The SHA-512 hash of the password to be verified.
* @return bool Returns true if the password matches the stored hash; false otherwise.
* @throws CryptographyException If the password hash is invalid or an error occurs during the cryptographic operation.
* @throws DatabaseOperationException If an error occurs during the database operation.
*/
public static function verifyPassword(string|RegisteredPeerRecord $peerUuid, string $hash): bool
public static function verifyPassword(string|RegisteredPeerRecord $peerUuid, string $sha512): bool
{
if($peerUuid instanceof RegisteredPeerRecord)
{
$peerUuid = $peerUuid->getUuid();
}
Cryptography::validatePasswordHash($hash);
try
{
$stmt = Database::getConnection()->prepare('SELECT hash FROM authentication_passwords WHERE peer_uuid=:uuid');
@ -190,7 +194,7 @@
throw new CryptographyException('Cannot decrypt hashed password');
}
return Cryptography::verifyPassword($hash, $decryptedHash);
return Cryptography::verifyPassword($sha512, $decryptedHash);
}
catch(PDOException $e)
{