2020-05-18 14:14:28 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2020-03-26 19:04:51 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Application.Security.Commands.GenerateKeyFile
|
|
|
|
|
{
|
|
|
|
|
public class GenerateKeyFileCommand : IRequest
|
|
|
|
|
{
|
|
|
|
|
public string KeyFilePath { get; set; }
|
|
|
|
|
public bool AddAdditionalEntropy { get; set; }
|
|
|
|
|
|
|
|
|
|
public class GenerateKeyFileCommandHandler : IAsyncRequestHandler<GenerateKeyFileCommand>
|
|
|
|
|
{
|
2020-04-20 20:02:43 +02:00
|
|
|
|
private readonly ICredentialsProxy _security;
|
2020-03-26 19:04:51 +01:00
|
|
|
|
private readonly IFileProxy _file;
|
2020-05-18 14:14:28 +02:00
|
|
|
|
private readonly ICryptographyClient _cryptography;
|
2020-03-26 19:04:51 +01:00
|
|
|
|
|
2020-05-18 14:14:28 +02:00
|
|
|
|
public GenerateKeyFileCommandHandler(ICredentialsProxy security, IFileProxy file, ICryptographyClient cryptography)
|
2020-03-26 19:04:51 +01:00
|
|
|
|
{
|
|
|
|
|
_security = security;
|
|
|
|
|
_file = file;
|
2020-05-18 14:14:28 +02:00
|
|
|
|
_cryptography = cryptography;
|
2020-03-26 19:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Handle(GenerateKeyFileCommand message)
|
|
|
|
|
{
|
|
|
|
|
byte[] entropy = null;
|
|
|
|
|
if (message.AddAdditionalEntropy)
|
|
|
|
|
{
|
2020-05-18 14:14:28 +02:00
|
|
|
|
entropy = _cryptography.Random(10);
|
2020-03-26 19:04:51 +01:00
|
|
|
|
}
|
|
|
|
|
var keyFile = _security.GenerateKeyFile(entropy);
|
|
|
|
|
await _file.WriteBinaryContentsToFile(message.KeyFilePath, keyFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|