WIP Protect/Unprotect Additional Field on selection

This commit is contained in:
Geoffroy BONNEVILLE
2020-05-18 22:20:31 +02:00
parent 9126307b4c
commit b7f8853ef2
17 changed files with 227 additions and 75 deletions

View File

@@ -253,7 +253,8 @@ namespace ModernKeePass.Infrastructure.KeePass
case EntryFieldName.Password:
case EntryFieldName.Notes:
case EntryFieldName.Url:
var unprotectedFieldValue = isProtected ? await _cryptography.UnProtect(fieldValue.ToString()) : fieldValue.ToString();
var stringValue = fieldValue == null ? string.Empty: fieldValue.ToString();
var unprotectedFieldValue = isProtected ? await _cryptography.UnProtect(stringValue) : stringValue;
pwEntry.Strings.Set(EntryFieldMapper.MapFieldToPwDef(fieldName), new ProtectedString(isProtected, unprotectedFieldValue));
break;
case EntryFieldName.HasExpirationDate:
@@ -272,7 +273,8 @@ namespace ModernKeePass.Infrastructure.KeePass
pwEntry.ForegroundColor = (Color)fieldValue;
break;
default:
var unprotectedAdditionalFieldValue = isProtected ? await _cryptography.UnProtect(fieldValue.ToString()) : fieldValue.ToString();
var additionalStringValue = fieldValue == null ? string.Empty: fieldValue.ToString();
var unprotectedAdditionalFieldValue = isProtected ? await _cryptography.UnProtect(additionalStringValue) : additionalStringValue;
pwEntry.Strings.Set(fieldName, new ProtectedString(isProtected, unprotectedAdditionalFieldValue));
break;
}

View File

@@ -74,6 +74,24 @@ namespace ModernKeePass.Infrastructure.UWP
return result;
}
public async Task WriteToLogFile(IEnumerable<string> data)
{
var local = ApplicationData.Current.LocalFolder;
var logFile = await local.CreateFileAsync("LogFile.txt", CreationCollisionOption.OpenIfExists).AsTask();
if (logFile != null)
{
try
{
await FileIO.AppendLinesAsync(logFile, data);
}
catch (Exception)
{
// If another option is available to the app to log error(i.e. Azure Mobile Service, etc...) then try that here
}
}
}
public void ReleaseFile(string path)
{
StorageApplicationPermissions.FutureAccessList.Remove(path);

View File

@@ -12,27 +12,21 @@ namespace ModernKeePass.Infrastructure.UWP
public async Task<string> Protect(string value)
{
if (string.IsNullOrEmpty(value)) return value;
try
{
// Create a DataProtectionProvider object for the specified descriptor.
var provider = new DataProtectionProvider("LOCAL=user");
// Encode the plaintext input message to a buffer.
var buffMsg = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
// Encrypt the message.
var buffProtected = await provider.ProtectAsync(buffMsg).AsTask().ConfigureAwait(false);
// Encode buffer to Base64
var protectedValue = CryptographicBuffer.EncodeToBase64String(buffProtected);
// Create a DataProtectionProvider object for the specified descriptor.
var provider = new DataProtectionProvider("LOCAL=user");
// Encode the plaintext input message to a buffer.
var buffMsg = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
// Return the encrypted string.
return protectedValue;
}
catch (Exception e)
{
return string.Empty;
}
// Encrypt the message.
var buffProtected = await provider.ProtectAsync(buffMsg).AsTask().ConfigureAwait(false);
// Encode buffer to Base64
var protectedValue = CryptographicBuffer.EncodeToBase64String(buffProtected);
// Return the encrypted string.
return protectedValue;
}
public async Task<string> UnProtect(string value)