// Copyright (C) 2025, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Security.Cryptography;
#nullable enable
namespace Duplicati.Server;
///
/// This class is used to store the PBKDF configuration parameters
///
public record PbkdfConfig(string Algorithm, int Version, string Salt, int Iterations, string HashAlorithm, string Hash)
{
///
/// The version to embed in the configuration
///
private const int _Version = 1;
///
/// The algorithm to use
///
private const string _Algorithm = "PBKDF2";
///
/// The hash algorithm to use
///
private const string _HashAlorithm = "SHA256";
///
/// The number of iterations to use
///
private const int _Iterations = 10000;
///
/// The size of the hash
///
private const int _HashSize = 32;
///
/// Creates a default PBKDF2 configuration
///
public static PbkdfConfig Default => new PbkdfConfig(_Algorithm, _Version, string.Empty, _Iterations, _HashAlorithm, string.Empty);
///
/// Creates a new PBKDF2 configuration with a random salt
///
/// The password to hash
public static PbkdfConfig CreatePBKDF2(string password)
{
var prng = RandomNumberGenerator.Create();
var buf = new byte[_HashSize];
prng.GetBytes(buf);
var salt = Convert.ToBase64String(buf);
var pwd = Convert.ToBase64String(Rfc2898DeriveBytes.Pbkdf2(password, buf, _Iterations, new HashAlgorithmName(_HashAlorithm), _HashSize));
return new PbkdfConfig(_Algorithm, _Version, salt, _Iterations, _HashAlorithm, pwd);
}
///
/// Calculates the hash for the given password using the current configuration
///
/// The password to hash
/// The hashed password
private string ComputeHash(string password)
{
return Convert.ToBase64String(Rfc2898DeriveBytes.Pbkdf2(password, Convert.FromBase64String(Salt), Iterations, new HashAlgorithmName(HashAlorithm), _HashSize));
}
///
/// Creates a new PBKDF2 configuration with the given password
///
/// The password to use
/// The updated PBKDF2 configuration
public PbkdfConfig WithPassword(string password)
=> WithHash(ComputeHash(password));
///
/// Creates a new PBKDF2 configuration with the given hash
///
/// The hash to use
/// The updated PBKDF2 configuration
public PbkdfConfig WithHash(string hash)
=> this with { Hash = hash };
///
/// Verifies a password against a PBKDF2 configuration
///
/// The password to verify
/// True if the password matches the configuration
public bool VerifyPassword(string password)
=> CryptographicOperations.FixedTimeEquals(Convert.FromBase64String(Hash), Convert.FromBase64String(ComputeHash(password)));
}