mirror of
https://github.com/KeyKeeperApp/KeyKeeper.git
synced 2026-04-28 19:06:37 +03:00
add CompositeKey class with a hashing method
This commit is contained in:
34
src/KeyKeeper/PasswordStore/Crypto/CompositeKey.cs
Normal file
34
src/KeyKeeper/PasswordStore/Crypto/CompositeKey.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user