make salt field optional in CompositeKey so it can be set later by the callee

This commit is contained in:
2025-12-02 19:20:46 +03:00
parent 4fbebbbf80
commit f8ddcddc6f

View File

@@ -7,22 +7,39 @@ namespace KeyKeeper.PasswordStore.Crypto;
public class CompositeKey
{
public string Password { get; }
public byte[] Salt { get; }
public byte[]? Salt
{
get { return salt; }
set
{
if (salt == null)
salt = value;
}
}
public CompositeKey(string password, byte[] salt)
private byte[]? salt;
public bool CanComputeHash
{
get { return salt != null; }
}
public CompositeKey(string password, byte[]? salt)
{
if (password == null)
throw new ArgumentNullException("password");
Password = password;
if (salt == null || salt.Length < FileFormatConstants.MIN_MASTER_SALT_LEN ||
salt.Length > FileFormatConstants.MAX_MASTER_SALT_LEN)
if (salt != null && (salt.Length < FileFormatConstants.MIN_MASTER_SALT_LEN ||
salt.Length > FileFormatConstants.MAX_MASTER_SALT_LEN))
throw new ArgumentException("salt");
Salt = salt;
this.salt = salt;
}
public byte[] Hash()
{
if (!CanComputeHash)
throw new InvalidOperationException("salt is not set");
byte[] passwordBytes = Encoding.UTF8.GetBytes(Password);
byte[] hashedString = new byte[passwordBytes.Length + Salt.Length * 2];
Salt.CopyTo(hashedString, 0);