add CompositeKey class with a hashing method

This commit is contained in:
2025-11-25 20:47:00 +03:00
parent a2f5ccf64b
commit 384a303a1d

View File

@@ -0,0 +1,34 @@
using System;
using System.Security.Cryptography;
using System.Text;
namespace KeyKeeper.PasswordStore.Crypto;
public struct CompositeKey
{
public string Password { get; }
public byte[] Salt { get; }
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)
throw new ArgumentException("salt");
Salt = salt;
}
public byte[] Hash()
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(Password);
byte[] hashedString = new byte[passwordBytes.Length + Salt.Length * 2];
Salt.CopyTo(hashedString, 0);
passwordBytes.CopyTo(hashedString, Salt.Length);
Salt.CopyTo(hashedString, Salt.Length + passwordBytes.Length);
return SHA256.HashData(hashedString);
}
}