From 384a303a1dd88954ac9da2a9a42015608daeaa0d Mon Sep 17 00:00:00 2001 From: Slavasil Date: Tue, 25 Nov 2025 20:47:00 +0300 Subject: [PATCH] add CompositeKey class with a hashing method --- .../PasswordStore/Crypto/CompositeKey.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/KeyKeeper/PasswordStore/Crypto/CompositeKey.cs diff --git a/src/KeyKeeper/PasswordStore/Crypto/CompositeKey.cs b/src/KeyKeeper/PasswordStore/Crypto/CompositeKey.cs new file mode 100644 index 0000000..43ba1ad --- /dev/null +++ b/src/KeyKeeper/PasswordStore/Crypto/CompositeKey.cs @@ -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); + } +}